javascript 从星期一开始一周的 isoWeekday()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18875649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting the week on Monday with isoWeekday()
提问by savinger
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 isoWeekdaymethod.
我正在创建一个日历,在其中以表格格式打印出几周。一个要求是我可以根据某些用户选项在星期一或星期日开始这几周。我很难使用 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);
}
EDITI 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。
回答by tetri
try using begin.startOf('isoWeek');
instead of begin.startOf('week');
尝试使用begin.startOf('isoWeek');
代替begin.startOf('week');
回答by letiagoalves
Call startOf
before isoWeekday
.
startOf
之前打电话isoWeekday
。
var begin = moment(date).startOf('week').isoWeekday(1);
回答by Roberto Aguilar
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)
回答by davethecoder
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');
回答by Hinrich
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);
回答by Adam Reis
For those who want isoWeek
to be the default you can modify moment's behaviour as such:
对于那些想要isoWeek
成为默认值的人,您可以像这样修改 moment 的行为:
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().subtract(1, period).startOf(period);
. 这不适用于 period is isoWeek
。