Moment.js moment().isoWeekday()方法
moment().isoWeekday()方法 用于获取或设置Moment对象的ISO工作日。ISO周编号系统在其系统中考虑了闰周。这样可以只有52或53个完整的周。这是通过将天数视为364或371天而不是365或366天来实现的。
语法:
moment().isoWeekday( Number|String );
参数: 此方法接受一个单一参数,如上所述并以下所述:
- Number|String: 它是要设置的Moment对象的ISO星期几。它是一个可选参数。
返回值: 此方法返回Moment的当前ISO星期几。
注意: 在普通的Node.js程序中,这将无法工作,因为它需要全局安装或在项目目录中安装外部的moment.js库。
您可以使用以下命令安装Moment.js:
安装moment模块:
npm install moment
下面的示例将演示Moment.js的moment().isoWeekday()方法。 moment().isoWeekday()方法 。
示例1:
const moment = require('moment');
console.log("Current Date:", moment().toString())
console.log("Current isoWeekday is:", moment().isoWeekday())
let thisWeekMonday = moment().isoWeekday(1);
console.log(
"This week's Monday is:",
thisWeekMonday.toString()
)
let thisWeekTuesday = moment().isoWeekday(2);
console.log(
"This week's Tuesday is:",
thisWeekTuesday.toString()
)
let thisWeekFriday = moment().isoWeekday("Friday");
console.log(
"This week's Friday is:",
thisWeekFriday.toString()
)
let thisWeekSunday = moment().isoWeekday("Sun");
console.log(
"This week's Sunday is:",
thisWeekSunday.toString()
)
输出:
当前日期:Mon Jul 18 2022 01:38:52 GMT+0530
当前 isoWeekday 是:1
本周的星期一是:Mon Jul 18 2022 01:38:52 GMT+0530
本周的星期二是:Tue Jul 19 2022 01:38:52 GMT+0530
本周的星期五是:Fri Jul 22 2022 01:38:52 GMT+0530
本周的星期日是:Sun Jul 24 2022 01:38:52 GMT+0530
示例2:
const moment = require('moment');
console.log("Current Date:", moment().toString())
console.log("Current isoWeekday is:",
moment().isoWeekday())
// Next week is 7 (full week) + 2 (for tuesday) = 9
let nextWeekTuesday = moment().isoWeekday(9);
console.log(
"Next week's Tuesday is:",
nextWeekTuesday.toString()
)
// Previous week is 2 (for tuesday) - 7 (full week) = -5
let prevWeekTuesday = moment().isoWeekday(-5);
console.log(
"Previous week's Tuesday is:",
prevWeekTuesday.toString()
)
// Next week is 14 (2 full weeks) + 7 (for sunday) = 14
let nextToNextWeekSunday = moment().isoWeekday(21);
console.log(
"Next to next week's Sunday is:",
nextToNextWeekSunday.toString()
)
输出:
当前日期: 周一,2022年7月18日 01:38:52 GMT+0530
当前星期几是: 1
下周的星期二是: 周二,2022年7月26日 01:38:52 GMT+0530
上周的星期二是: 周二,2022年7月12日 01:38:52 GMT+0530
下下周的星期日是: 周日,2022年8月7日 01:38:52 GMT+0530
参考: https://momentjs.com/docs/#/get-set/iso-weekday/