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


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:

Javascript


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:

Javascript


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/



相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Moment.js moment().weeksInYear() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。