Moment.js moment().isoWeekYear() 方法
moment().isoWeekYear() 方法 用于获取或设置 Moment 对象的 ISO 周年份。ISO 周编号系统在其系统中考虑了闰周。这使其只能有 52 或 53 个完整周。这是通过将天数视为 364 或 371 天而不是 365 或 366 天来实现的。这使得该方法无论地区如何返回相同的日期。
语法:
moment().isoWeekYear( Number );
参数: 此方法接受一个参数,如上所述并以下所述:
- Number: 这是要设置为Moment对象的ISO周年份。它是一个可选参数。
返回值: 此方法返回Moment的当前ISO周。
注意: 这在普通Node.js程序中不起作用,因为它需要全局或项目目录中安装外部moment.js库。
Moment.js可以使用以下命令安装:
moment模块的安装:
npm install moment
下面的示例将演示 Moment.js 的 moment().isoWeekYear() 方法 。
示例1:
const moment = require('moment');
console.log("Current Date:", moment().toString())
console.log("Current isoWeekYear is:", moment().isoWeekYear())
let isoWeekYear1 = moment().isoWeekYear(1);
console.log(
"Moment with isoWeekYear of 1 is:",
isoWeekYear1.toString()
)
let isoWeekYear2 = moment().isoWeekYear(2);
console.log(
"Moment with isoWeekYear of 2 is:",
isoWeekYear2.toString()
)
let isoWeekYear2010 = moment().isoWeekYear(2010);
console.log(
"Moment with isoWeekYear of 2010 is:",
isoWeekYear2010.toString()
)
let isoWeekYear2024 = moment().isoWeekYear(2024);
console.log(
"Moment with isoWeekYear of 2024 is:",
isoWeekYear2024.toString()
)
输出:
当前日期: Mon Jul 18 2022 01:04:05 GMT+0530
当前 isoWeekYear 是: 2022
isoWeekYear 为 1 的 Moment 是: Mon Jul 16 0001 01:04:05 GMT+0553
isoWeekYear 为 2 的 Moment 是: Mon Jul 15 0002 01:04:05 GMT+0553
isoWeekYear 为 2010 的 Moment 是: Mon Jul 19 2010 01:04:05 GMT+0530
isoWeekYear 为 2024 的 Moment 是: Mon Jul 15 2024 01:04:05 GMT+0530
示例2:
const moment = require('moment');
console.log("Current Date:", moment().toString())
console.log(
"Current isoWeekYear is:",
moment().isoWeekYear()
)
let isoWeekYearDecember = moment().isoWeekYear(-100);
console.log(
"Moment with isoWeekYear of -100 is:",
isoWeekYearDecember.toString()
)
let isoWeekYearFeb = moment().isoWeekYear(-2020);
console.log(
"Moment with isoWeekYear of -2020 is:",
isoWeekYearFeb.toString()
)
输出:
Current Date:Mon Jul 18 2022 01:04:05 GMT+0530
Current isoWeekYear is:2022
Moment with isoWeekYear of -100 is:Mon Jul 16 -0100 01:04:05 GMT+0553
Moment with isoWeekYear of -2020 is:Mon Jul 14 -2020 01:04:05 GMT+0553
参考: https://momentjs.com/docs/#/get-set/iso-week-year/