當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。