Moment.js moment().isoWeeksInYear() 方法
moment().isoWeeksInYear() 方法 用于返回 Moment 对象当前年份的 ISO 周数。ISO 周编号系统考虑了闰周。这使得该系统只有 52 或 53 个完整周。这是通过将一年的天数视为 364 或 371 天而不是 365 或 366 天来实现的。
语法:
moment().isoWeeksInYear();
参数: 此方法不接受任何参数。
返回值: 此方法返回当前 Moment 对象所在年份的 ISO 周数。
注意: 在普通的 Node.js 程序中,此方法无法正常工作,因为需要全局安装或在项目目录中安装外部的 moment.js 库。
moment.js 可以使用以下命令进行安装:
moment 模块的安装:
npm install moment
下面的示例将演示 Moment.js moment().isoWeeksInYear()方法 。
示例1:
const moment = require('moment');
console.log("Current Date:", moment().toString())
console.log("Current year is:", moment().year())
console.log(
"ISO Weeks in current year is:",
moment().isoWeeksInYear()
)
let year2040 = moment().year(2040);
console.log(
"ISO Weeks in Year 2040 is:",
year2040.isoWeeksInYear()
)
let year1975 = moment().year(1975);
console.log(
"ISO Weeks in Year 1975 is:",
year1975.isoWeeksInYear()
)
输出:
Current Date: Wed Jul 13 2022 00:46:56 GMT+0530
Current year is: 2022
ISO Weeks in current year is: 52
ISO Weeks in Year 2040 is: 52
ISO Weeks in Year 1975 is: 52
示例2:
const moment = require('moment');
for (let i = 2020; i < 2040; i++) {
console.log(
`ISO Weeks in Year {i} is:{moment().year(i).isoWeeksInYear()}`
)
}
输出:
ISO Weeks in Year 2020 is: 53
ISO Weeks in Year 2021 is: 52
ISO Weeks in Year 2022 is: 52
ISO Weeks in Year 2023 is: 52
ISO Weeks in Year 2024 is: 52
ISO Weeks in Year 2025 is: 52
ISO Weeks in Year 2026 is: 53
ISO Weeks in Year 2027 is: 52
ISO Weeks in Year 2028 is: 52
ISO Weeks in Year 2029 is: 52
ISO Weeks in Year 2030 is: 52
ISO Weeks in Year 2031 is: 52
ISO Weeks in Year 2032 is: 53
ISO Weeks in Year 2033 is: 52
ISO Weeks in Year 2034 is: 52
ISO Weeks in Year 2035 is: 52
ISO Weeks in Year 2036 is: 52
ISO Weeks in Year 2037 is: 53
ISO Weeks in Year 2038 is: 52
ISO Weeks in Year 2039 is: 52
参考: https://momentjs.com/docs/#/get-set/iso-weeks-in-year/