Moment.js - 周一以isoWeekday()开始一周

时间:2022-09-26 11:14:31

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.

我正在创建一个日历,我以表格格式打印周。一个要求是,我可以根据某些用户选项在星期一或星期日开始这几周。我很难使用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);
}

jsFiddle

的jsfiddle

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.

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

6 个解决方案

#1


156  

try using begin.startOf('isoWeek'); instead of begin.startOf('week');

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

#2


7  

Call startOf before isoWeekday.

在isoWeekday之前调用startOf。

var begin = moment(date).startOf('week').isoWeekday(1);

Working demo

工作演示

#3


2  

thought I would add this for any future peeps. It will always make sure that its monday if needed, can also be used to always ensure sunday. For me I always need monday, but local is dependant on the machine being used, and this is an easy fix:

以为我会为未来的窥视添加这个。它总是会确保它的星期一如果需要,也可以用来始终确保星期日。对我来说,我总是需要星期一,但本地依赖于正在使用的机器,这是一个简单的解决方案:

var begin = moment().isoWeekday(1).startOf('week');
var begin2 = moment().startOf('week');
// could check to see if day 1 = Sunday  then add 1 day
// my mac on bst still treats day 1 as sunday    

var firstDay = moment().startOf('week').format('dddd') === 'Sunday' ?     
moment().startOf('week').add('d',1).format('dddd DD-MM-YYYY') : 
moment().startOf('week').format('dddd DD-MM-YYYY');

document.body.innerHTML = '<b>could be monday or sunday depending on client: </b><br />' + 
begin.format('dddd DD-MM-YYYY') + 
'<br /><br /> <b>should be monday:</b> <br>' + firstDay + 
'<br><br> <b>could also be sunday or monday </b><br> ' + 
begin2.format('dddd DD-MM-YYYY');

#4


0  

Here is a more generic solution for any given weekday. Working demo on jsfiddle

对于任何给定的工作日,这是一个更通用的解决方案。关于jsfiddle的工作演示

var myIsoWeekDay = 2; // say our weeks start on tuesday, for monday you would type 1, etc.

var startOfPeriod = moment("2013-06-23T00:00:00"),

// how many days do we have to substract?
var daysToSubtract = moment(startOfPeriod).isoWeekday() >= myIsoWeekDay ?
    moment(startOfPeriod).isoWeekday() - myIsoWeekDay :
    7 + moment(startOfPeriod).isoWeekday() - myIsoWeekDay;

// subtract days from start of period
var begin = moment(startOfPeriod).subtract('d', daysToSubtract);

#5


0  

This way you can set the initial day of the week.

这样您就可以设置一周的初始日期。

moment.locale('en', {
    week: {
        dow: 6
    }
});
moment.locale('en');

Make sure to use it with moment().weekday(1); instead of moment.isoWeekday(1)

确保使用moment()。weekday(1);而不是moment.isoWeekday(1)

#6


0  

For those who want isoWeek to be the default you can modify moment's behaviour as such:

对于那些希望isoWeek成为默认值的人,你可以修改时刻的行为:

const moment = require('moment');
const proto = Object.getPrototypeOf(moment());

const {startOf, endOf} = proto;
proto.startOf = function(period) {
  if (period === 'week') {
    period = 'isoWeek';
  }
  return startOf.call(this, period);
};
proto.endOf = function(period) {
  if (period === 'week') {
    period = 'isoWeek';
  }
  return endOf.call(this, period);
};

Now you can simply use someDate.startOf('week') without worrying you'll get sunday or having to think about whether to use isoweek or isoWeek etc.

现在你可以简单地使用someDate.startOf('week')而不用担心你会得到星期天或者不得不考虑是否使用isoweek或isoWeek等。

Plus you can store this in a variable like const period = 'week' and use it safely in subtract() or add() operations, e.g. moment().subtract(1, period).startOf(period);. This won't work with period being isoWeek.

另外,您可以将其存储在const period ='week'之类的变量中,并在subtract()或add()操作中安全地使用它,例如: moment()。减法(1,句号).startOf(句号);.这不适用于期间为isoWeek。

#1


156  

try using begin.startOf('isoWeek'); instead of begin.startOf('week');

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

#2


7  

Call startOf before isoWeekday.

在isoWeekday之前调用startOf。

var begin = moment(date).startOf('week').isoWeekday(1);

Working demo

工作演示

#3


2  

thought I would add this for any future peeps. It will always make sure that its monday if needed, can also be used to always ensure sunday. For me I always need monday, but local is dependant on the machine being used, and this is an easy fix:

以为我会为未来的窥视添加这个。它总是会确保它的星期一如果需要,也可以用来始终确保星期日。对我来说,我总是需要星期一,但本地依赖于正在使用的机器,这是一个简单的解决方案:

var begin = moment().isoWeekday(1).startOf('week');
var begin2 = moment().startOf('week');
// could check to see if day 1 = Sunday  then add 1 day
// my mac on bst still treats day 1 as sunday    

var firstDay = moment().startOf('week').format('dddd') === 'Sunday' ?     
moment().startOf('week').add('d',1).format('dddd DD-MM-YYYY') : 
moment().startOf('week').format('dddd DD-MM-YYYY');

document.body.innerHTML = '<b>could be monday or sunday depending on client: </b><br />' + 
begin.format('dddd DD-MM-YYYY') + 
'<br /><br /> <b>should be monday:</b> <br>' + firstDay + 
'<br><br> <b>could also be sunday or monday </b><br> ' + 
begin2.format('dddd DD-MM-YYYY');

#4


0  

Here is a more generic solution for any given weekday. Working demo on jsfiddle

对于任何给定的工作日,这是一个更通用的解决方案。关于jsfiddle的工作演示

var myIsoWeekDay = 2; // say our weeks start on tuesday, for monday you would type 1, etc.

var startOfPeriod = moment("2013-06-23T00:00:00"),

// how many days do we have to substract?
var daysToSubtract = moment(startOfPeriod).isoWeekday() >= myIsoWeekDay ?
    moment(startOfPeriod).isoWeekday() - myIsoWeekDay :
    7 + moment(startOfPeriod).isoWeekday() - myIsoWeekDay;

// subtract days from start of period
var begin = moment(startOfPeriod).subtract('d', daysToSubtract);

#5


0  

This way you can set the initial day of the week.

这样您就可以设置一周的初始日期。

moment.locale('en', {
    week: {
        dow: 6
    }
});
moment.locale('en');

Make sure to use it with moment().weekday(1); instead of moment.isoWeekday(1)

确保使用moment()。weekday(1);而不是moment.isoWeekday(1)

#6


0  

For those who want isoWeek to be the default you can modify moment's behaviour as such:

对于那些希望isoWeek成为默认值的人,你可以修改时刻的行为:

const moment = require('moment');
const proto = Object.getPrototypeOf(moment());

const {startOf, endOf} = proto;
proto.startOf = function(period) {
  if (period === 'week') {
    period = 'isoWeek';
  }
  return startOf.call(this, period);
};
proto.endOf = function(period) {
  if (period === 'week') {
    period = 'isoWeek';
  }
  return endOf.call(this, period);
};

Now you can simply use someDate.startOf('week') without worrying you'll get sunday or having to think about whether to use isoweek or isoWeek etc.

现在你可以简单地使用someDate.startOf('week')而不用担心你会得到星期天或者不得不考虑是否使用isoweek或isoWeek等。

Plus you can store this in a variable like const period = 'week' and use it safely in subtract() or add() operations, e.g. moment().subtract(1, period).startOf(period);. This won't work with period being isoWeek.

另外,您可以将其存储在const period ='week'之类的变量中,并在subtract()或add()操作中安全地使用它,例如: moment()。减法(1,句号).startOf(句号);.这不适用于期间为isoWeek。