Moment.js moment().weeksInYear() 方法
moment().weeksInYear() 方法 用于返回Moment对象的当前年份中一共有多少周。该方法与所使用的语言环境相关,因此结果可能因Moment对象的当前语言环境而异。
语法:
moment().weeksInYear();
参数:
此方法不接受任何参数。
返回值:
此方法返回 Moment 对象当前年份的周数。
注意:
此方法在普通的 Node.js 程序中不起作用,因为它需要全局安装或在项目目录中安装一个外部 moment.js 库。
可以使用以下命令安装 Moment.js:
安装 moment 模块:
npm install moment
以下示例将演示 Moment.js moment().weeksInYear() 方法 。
示例1:
const moment = require('moment');
console.log("Current Date:", moment().toString())
console.log("Current year is:", moment().year())
let year2050 = moment().year(2050);
console.log(
"Weeks in Year 2050 is:",
year2050.weeksInYear()
)
let year1950 = moment().year(1950);
console.log(
"Weeks in Year 1950 is:",
year1950.weeksInYear()
)
输出:
Current Date: Mon Jul 11 2022 02:14:41 GMT+0530
Current year is: 2022
Weeks in Year 2050 is: 53
Weeks in Year 1950 is: 52
示例2:
const moment = require('moment');
console.log("Current Date:", moment().toString())
console.log("Current year is:", moment().year())
for (let i = 2000; i < 2030; i++) {
console.log(
`Weeks in Year {i} is:{moment().year(i).weeksInYear()}`
)
}
输出:
Weeks in Year 2000 is: 53
Weeks in Year 2001 is: 52
Weeks in Year 2002 is: 52
Weeks in Year 2003 is: 52
Weeks in Year 2004 is: 52
Weeks in Year 2005 is: 53
Weeks in Year 2006 is: 52
Weeks in Year 2007 is: 52
Weeks in Year 2008 is: 52
Weeks in Year 2009 is: 52
Weeks in Year 2010 is: 52
Weeks in Year 2011 is: 53
Weeks in Year 2012 is: 52
Weeks in Year 2013 is: 52
Weeks in Year 2014 is: 52
Weeks in Year 2015 is: 52
Weeks in Year 2016 is: 53
Weeks in Year 2017 is: 52
Weeks in Year 2018 is: 52
Weeks in Year 2019 is: 52
Weeks in Year 2020 is: 52
Weeks in Year 2021 is: 52
Weeks in Year 2022 is: 53
Weeks in Year 2023 is: 52
Weeks in Year 2024 is: 52
Weeks in Year 2025 is: 52
Weeks in Year 2026 is: 52
Weeks in Year 2027 is: 52
Weeks in Year 2028 is: 53
Weeks in Year 2029 is: 52
参考:https://momentjs.com/docs/#/get-set/weeks-in-year/