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/
相关用法
- Moment.js moment().isoWeeksInYear()用法及代码示例
- Moment.js moment().isoWeek()用法及代码示例
- Moment.js moment().isoWeekYear()用法及代码示例
- Moment.js moment().inspect()用法及代码示例
- Moment.js moment().invalid()用法及代码示例
- Moment.js moment().calender()用法及代码示例
- Moment.js moment().daysInMonth()用法及代码示例
- Moment.js moment().diff()用法及代码示例
- Moment.js moment().format()用法及代码示例
- Moment.js moment().from()用法及代码示例
- Moment.js moment().fromNow()用法及代码示例
- Moment.js moment().to()用法及代码示例
- Moment.js moment().toNow()用法及代码示例
- Moment.js moment().unix()用法及代码示例
- Moment.js moment().valueOf()用法及代码示例
- Moment.js moment().millisecond()用法及代码示例
- Moment.js moment().set()用法及代码示例
- Moment.js moment().toString()用法及代码示例
- Moment.js moment().local()用法及代码示例
- Moment.js moment().min()用法及代码示例
- Moment.js moment().get()用法及代码示例
- Moment.js moment().startOf()用法及代码示例
- Moment.js moment().toArray()用法及代码示例
- Moment.js moment().utcOffset()用法及代码示例
- Moment.js moment().zone()用法及代码示例
注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Moment.js moment().isoWeekday() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。