我正在创建一个日历,以表格格式打印出几周。一个要求是我可以根据某些用户选项在星期一或星期日开始一周。我很难使用 moment 的isoWeekday方法。
// Start of some date range. Can be any day of the week.
var startOfPeriod = moment("2013-06-23T00:00:00"),
// We begin on the start of the first week.
// Mon Tues Wed Thur Fri Sat Sun
// 20 21 22 23 24 25 26
begin = moment(startOfPeriod).isoWeekday(1); // will pull from user setting
console.log(begin.isoWeekday()); // 1 - all good
// Let's get the beginning of this first week, respecting the isoWeekday
begin.startOf('week');
console.log(begin.isoWeekday()); // 7 - what happened ???
// Get column headers
for (var i=0; i<7; i++) {
console.log(begin.format('ddd')); // I want Monday first!
begin.add('d', 1);
}
编辑我误解了isoWeekday
实际在做什么。我认为它设置了“一周中的哪一天是一周的第一天”变量(不存在)。它实际上所做的只是更改星期几,就像 一样moment.weekday()
,但使用 1-7 范围而不是 0-6。