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


Moment.js moment().isoWeekday()用法及代碼示例


moment().isoWeekday()方法用於獲取或設置Moment對象的ISO工作日。 ISO week-numbering 係統在其係統中考慮閏周。這使得它隻有 52 或 53 整周。這是通過將天數考慮為 364 或 371 天而不是 365 或 366 天來實現的。

用法:

moment().isoWeekday( Number|String );

參數:該方法接受如上所述和如下所述的單個參數:

  • 數字|字符串:必須為 Moment 對象設置 ISO 工作日。它是一個可選參數。

返回值:此方法返回當前 ISO 工作日的時刻。

注意:這在普通的 Node.js 程序中不起作用,因為它需要全局安裝或在項目目錄中安裝外部moment.js 庫。

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

moment模塊的安裝:

npm install moment

以下示例將演示 Moment.js moment().isoWeekday() 方法。

示例 1:

Javascript


const moment = require('moment'); 
  
console.log("Current Date:", moment().toString()) 
console.log("Current isoWeekday is:", moment().isoWeekday()) 
  
let thisWeekMonday = moment().isoWeekday(1); 
console.log( 
    "This week's Monday is:", 
    thisWeekMonday.toString() 
) 
  
let thisWeekTuesday = moment().isoWeekday(2); 
console.log( 
    "This week's Tuesday is:", 
    thisWeekTuesday.toString() 
) 
  
let thisWeekFriday = moment().isoWeekday("Friday"); 
console.log( 
    "This week's Friday is:", 
    thisWeekFriday.toString() 
) 
  
let thisWeekSunday = moment().isoWeekday("Sun"); 
console.log( 
    "This week's Sunday is:", 
    thisWeekSunday.toString() 
)

輸出:

Current Date: Mon Jul 18 2022 01:38:52 GMT+0530
Current isoWeekday is: 1
This week’s Monday is: Mon Jul 18 2022 01:38:52 GMT+0530
This week’s Tuesday is: Tue Jul 19 2022 01:38:52 GMT+0530
This week’s Friday is: Fri Jul 22 2022 01:38:52 GMT+0530
This week’s Sunday is: Sun Jul 24 2022 01:38:52 GMT+0530

示例 2:

Javascript


const moment = require('moment'); 
  
console.log("Current Date:", moment().toString()) 
console.log("Current isoWeekday is:",  
    moment().isoWeekday()) 
  
// Next week is 7 (full week) + 2 (for tuesday) = 9 
let nextWeekTuesday = moment().isoWeekday(9); 
console.log( 
    "Next week's Tuesday is:", 
    nextWeekTuesday.toString() 
) 
  
// Previous week is 2 (for tuesday) - 7 (full week) = -5 
let prevWeekTuesday = moment().isoWeekday(-5); 
console.log( 
    "Previous week's Tuesday is:", 
    prevWeekTuesday.toString() 
) 
  
// Next week is 14 (2 full weeks) + 7 (for sunday) = 14 
let nextToNextWeekSunday = moment().isoWeekday(21); 
console.log( 
    "Next to next week's Sunday is:", 
    nextToNextWeekSunday.toString() 
)

輸出:

Current Date: Mon Jul 18 2022 01:38:52 GMT+0530
Current isoWeekday is: 1
Next week’s Tuesday is: Tue Jul 26 2022 01:38:52 GMT+0530
Previous week’s Tuesday is: Tue Jul 12 2022 01:38:52 GMT+0530
Next to next week’s Sunday is: Sun Aug 07 2022 01:38:52 GMT+0530

參考:https://momentjs.com/docs/#/get-set/iso-weekday/



相關用法


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