週を表形式で印刷するカレンダーを作成しています。1 つの要件は、一部のユーザー オプションに従って、月曜日または日曜日のいずれかに週を開始できることです。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()
が、0 ~ 6 の代わりに 1 ~ 7 の範囲を使用します。