使用 isoWeekday() 从星期一开始一周

2024-05-12

我正在创建一个日历,以表格格式打印几周。一个要求是我能够根据某些用户选项从周一或周日开始一周。我很难使用 moment 的平日 http://momentjs.com/docs/?codekitCB=401206465.298830#/get-set/iso-weekday/ method.

// 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);
}

jsFiddle http://jsfiddle.net/PxAUd/

EDIT我误解了什么isoWeekday实际上是在做。我认为它设置了“一周中的哪一天是一周中的第一天”变量(该变量不存在)。它实际上所做的只是改变星期几,就像moment.weekday(),但使用 1-7 范围而不是 0-6。


你只需要更换begin.startOf('isoWeek'); with begin.startOf('week');.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 isoWeekday() 从星期一开始一周 的相关文章

随机推荐