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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。