get next week start and end using jquery and moment js
Asked Answered
A

4

39

I searched for this question and found there is a no answer on Stackoverflow.. So I decided to answer it...

This question helps if you need to get the start/end of next/last week with Monday as start of week.

Antichrist answered 3/3, 2014 at 10:58 Comment(0)
A
13

I used moment js for this ... u can get it from 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; 
        }
Antichrist answered 3/3, 2014 at 10:58 Comment(1)
This doesn't work (anymore?) - in GetNextWeekStart if today is Tuesday I get the following: > today.format('ddd, D-M-Y'); OUTPUT: 'Tue, 22-11-2016' > daystoMonday = 0 - (today.isoWeekday() - 1) + 7; OUTPUT 6 > nextMonday = today.subtract('days', daystoMonday) > nextMonday.format('ddd, D-M-Y') OUTPUT 'Wed, 16-11-2016' Shouldn't nextMonday be today.add('days', daystoMonday) instead?Qualify
S
165

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

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.

Stav answered 4/12, 2014 at 22:55 Comment(4)
moment().endOf('week') should be moment().endOf('isoWeek')Intensify
Note that .startOf("week") is actually locale dependent, so it may not always be Sunday.Impersonal
I am trying to get last week first day by: moment().subtract(1, 'weeks').startOf('week') and I get sunday(which is ok), then I change my timezone to US&Canada and still get Sunday, when I expect to get Monday, why is that? @Dr.AculaElayne
@OffirPe'er I don't think that Monday is the beginning of the week in US/Canada - you should try it with France/Germany or other european countries which do have their weeks starting on Monday. BTW, this whole story of week start in moment.js is rotten to the core. Why not having a simple setter 'setWeekStartDay` or so instead of flawed locale which may not be the user's when running code server-side?Qualify
A
13

I used moment js for this ... u can get it from 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; 
        }
Antichrist answered 3/3, 2014 at 10:58 Comment(1)
This doesn't work (anymore?) - in GetNextWeekStart if today is Tuesday I get the following: > today.format('ddd, D-M-Y'); OUTPUT: 'Tue, 22-11-2016' > daystoMonday = 0 - (today.isoWeekday() - 1) + 7; OUTPUT 6 > nextMonday = today.subtract('days', daystoMonday) > nextMonday.format('ddd, D-M-Y') OUTPUT 'Wed, 16-11-2016' Shouldn't nextMonday be today.add('days', daystoMonday) instead?Qualify
C
6

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

Crutchfield answered 3/3, 2014 at 11:25 Comment(1)
Thank you! This seems to be the better (and more robust) way to do it. Especially because it supports locales.Gayomart
I
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)
Intracranial answered 22/8, 2018 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.