当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Moment.js moment().isoWeeksInYear()用法及代码示例


moment().isoWeeksInYear()方法用于返回Moment对象当前年份的ISO周数。 ISO week-numbering 系统在其系统中考虑闰周。这使得它只有 52 或 53 整周。这是通过将天数考虑为 364 或 371 天而不是 365 或 366 天来实现的。

用法:

moment().isoWeeksInYear();

Parameters: 该方法不接受任何参数。

返回值:此方法返回 Moment 对象当年的 ISO 周数。

注意:这在普通的 Node.js 程序中不起作用,因为它需要全局安装或在项目目录中安装外部moment.js 库。

Moment.js 可以使用以下命令安装:

moment模块的安装:

npm install moment

下面的例子将演示Moment.js moment().isoWeeksInYear()方法.

示例 1:

Javascript


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:

Javascript


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/



相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Moment.js moment().isoWeeksInYear() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。