我搜索了这个问题,发现Stackoverflow上没有答案。所以我决定回答它......
如果您需要将星期一作为周开始的下一周/上周的开始/结束,这个问题会有所帮助。
答案 0 :(得分:128)
派对有点晚了,但这是我发现表达周末开始/结束的最简单方法。根据{{3}},isoWeek
参数在星期一开始,而week
根据您的区域设置开始周(因此可能是星期日或星期一)。
本周:
moment().startOf('isoWeek')
moment().endOf('isoWeek')
下周:
moment().add(1, 'weeks').startOf('isoWeek')
moment().add(1, 'weeks').endOf('isoWeek')
上周:
moment().subtract(1, 'weeks').startOf('isoWeek')
moment().subtract(1, 'weeks').endOf('isoWeek')
我喜欢这些结构,因为它们非常易读。通过在subtract
或add
中指定所需的周数,您可以轻松地返回或转发任意数周的内容。
答案 1 :(得分:13)
我使用了瞬间js ...你可以从here获得它
/*
all functions return moment() object.. <br>
GetNextWeekStart().format('DD/MM/YYYY') to get 24/02/2014
*/
function GetNextWeekStart() {
var today = moment();
//edited part
var daystoMonday = 0 - (today.isoWeekday() - 1) + 7;
var nextMonday = today.subtract('days', daystoMonday);
return nextMonday;
}
function GetNextWeekEnd() {
var nextMonday = GetNextWeekStart();
var nextSunday = nextMonday.add('days', 6);
return nextSunday;
}
function GetLastWeekStart() {
var today = moment();
var daystoLastMonday = 0 - (1 - today.isoWeekday()) + 7;
var lastMonday = today.subtract('days', daystoLastMonday);
return lastMonday;
}
function GetLastWeekEnd() {
var lastMonday = GetLastWeekStart();
var lastSunday = lastMonday.add('days', 6);
return lastSunday;
}
答案 2 :(得分:5)
这在the lang file中指定,您可以包含lang/en-au.js
或lang/en-gb.js
文件并设置所需的语言标准。假设你在英国:
moment.lang('en-gb');
如果您不想使用自定义语言,可以将其更改为默认的美国语言环境:
moment.lang('en-custom', {
week: {
dow: 1,
doy: 6 // Adjust the first week of the year, depends on the country. For the US it's 6. For the UK, 4.
}
});
然后你可以这样做:
var date = '2014-03-24';
console.log('next start', moment(date).weekday(7).format('DD/MM/YYYY'));
console.log('next end', moment(date).weekday(13).format('DD/MM/YYYY'));
console.log('prev start', moment(date).weekday(-7).format('DD/MM/YYYY'));
console.log('prev end', moment(date).weekday(-1).format('DD/MM/YYYY'));
console.log('current start', moment(date).weekday(0).format('DD/MM/YYYY'));
console.log('current end', moment(date).weekday(6).format('DD/MM/YYYY'));
/*
next start 31/03/2014
next end 06/04/2014
prev start 17/03/2014
prev end 23/03/2014
current start 24/03/2014
current end 30/03/2014
*/
答案 3 :(得分:1)
//Last week (get current week array list from momentjs)
var sd = moment(currentWeekFd[0]).subtract(7, 'days').format();
var ed = moment(currentWeekEd[6]).subtract(7, 'days').format();
var lastWeekStratDay = moment(sd).format('YYYY-MM-DD');
var lastWeekEndDay = moment(ed).format('YYYY-MM-DD');
console.log(lastWeekStratDay +', '+ lastWeekEndDay)
//Next week
var sd = moment(currentWeekFd[0]).add(7, 'days').format();
var ed = moment(currentWeekEd[6]).add(7, 'days').format();
var nextWeekStratDay = moment(sd).format('YYYY-MM-DD');
var nextWeekEndDay = moment(ed).format('YYYY-MM-DD');
console.log(nextWeekStratDay +', '+ nextWeekEndDay)