简体   繁体   English

下周使用jquery和moment js开始和结束

[英]get next week start and end using jquery and moment js

I searched for this question and found there is a no answer on Stackoverflow.. So I decided to answer it... 我搜索了这个问题,发现Stackoverflow上没有答案..所以我决定回答它......

This question helps if you need to get the start/end of next/last week with Monday as start of week. 如果您需要将星期一作为周开始的下一个/上周的开始/结束,这个问题会有所帮助。

A little late to the party but here is the simplest way I've found to express starts/ends of weeks. 派对有点晚了,但这是我发现表达周的开始/结束的最简单的方法。 The isoWeek argument starts weeks on Monday according to the ISO 8601 , while week starts weeks depending on your locale (so probably either Sunday or Monday). 根据ISO 8601isoWeek参数在周一开始几周,而week开始几周取决于您的区域设置 (因此可能是周日或周一)。

This week: 本星期:

moment().startOf('isoWeek')
moment().endOf('isoWeek')

Next week: 下周:

moment().add(1, 'weeks').startOf('isoWeek')
moment().add(1, 'weeks').endOf('isoWeek')

Last week: 上个星期:

moment().subtract(1, 'weeks').startOf('isoWeek')
moment().subtract(1, 'weeks').endOf('isoWeek')

I like these constructions because they are incredibly readable. 我喜欢这些结构,因为它们非常易读。 It's also easy to go back or forward any number of weeks by specifying how many weeks you want in subtract or add . 通过指定subtractadd的周数,可以轻松地返回或转发任意数周。

I used moment js for this ... u can get it from here 我用了这个时刻js ...你可以从这里得到它

     /*
     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; 
        }

This is specified in the lang file , you can include the lang/en-au.js or lang/en-gb.js file and set the desired language standard. 这在lang文件中指定,您可以包含lang/en-au.jslang/en-gb.js文件并设置所需的语言标准。 Assume you're in the UK: 假设你在英国:

moment.lang('en-gb');

If you don't want to use a custom language, you can change it for the default US locale: 如果您不想使用自定义语言,可以将其更改为默认的美国语言环境:

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.
    }
});

Then you can do: 然后你可以这样做:

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
*/

http://jsfiddle.net/WGXxn/3/ http://jsfiddle.net/WGXxn/3/

    //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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM