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


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