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


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


moment().isoWeekYear() 方法用于获取或设置 Moment 对象的 ISO 周年。 ISO week-numbering 系统在其系统中考虑闰周。这使得它只有 52 或 53 整周。这是通过将天数考虑为 364 或 371 天而不是 365 或 366 天来实现的。这使得该方法返回相同的日期,无论其区域设置如何。

用法:

moment().isoWeekYear( Number );

参数:该方法接受如上所述和如下所述的单个参数:

  • Number:必须为 Moment 对象设置 ISO Week Year。它是一个可选参数。

返回值:此方法返回当前 ISO 周的时刻。

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

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

moment模块的安装:

npm install moment

以下示例将演示 Moment.js moment().isoWeekYear() 方法。

示例 1:

Javascript


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() 
)

输出:

Current Date: Mon Jul 18 2022 01:04:05 GMT+0530
Current isoWeekYear is: 2022
Moment with isoWeekYear of 1 is: Mon Jul 16 0001 01:04:05 GMT+0553
Moment with isoWeekYear of 2 is: Mon Jul 15 0002 01:04:05 GMT+0553
Moment with isoWeekYear of 2010 is: Mon Jul 19 2010 01:04:05 GMT+0530
Moment with isoWeekYear of 2024 is: Mon Jul 15 2024 01:04:05 GMT+0530

示例 2:

Javascript


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/



相关用法


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