Moment.js - 周一以isoWeekday()开始一周 [英] Moment.js - Starting the week on Monday with isoWeekday()

查看:1005
本文介绍了Moment.js - 周一以isoWeekday()开始一周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个日历,我以表格格式打印周。一个要求是,我可以根据某些用户选项在周一或周日开始这几周。我很难使用moment的 isoWeekday 方法。

I'm creating a calendar where I print out weeks in a tabular format. One requirement is that I be able to start the weeks either on Monday or Sunday, as per some user option. I'm having a hard time using moment's isoWeekday 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

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

EDIT I misunderstood what isoWeekday was actually doing. I thought it set the "which day of the week is the first day of the week" variable (that doesn't exist). What it actually does is simply changes the day of the week, just like moment.weekday(), but uses a 1-7 range instead of the 0-6.

推荐答案

尝试使用 begin.startOf('isoWeek'); 而不是 begin.startOf('week');

这篇关于Moment.js - 周一以isoWeekday()开始一周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆