38

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.

Venkata K. C. Tata
  • 5,539
  • 4
  • 22
  • 35

4 Answers4

163

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.

Dr. Acula
  • 2,392
  • 4
  • 17
  • 17
  • moment().endOf('week') should be moment().endOf('isoWeek') – Vitall Dec 25 '14 at 04:55
  • 1
    Note that `.startOf("week")` is actually locale dependent, so it may not always be Sunday. – Gigo Jan 21 '16 at 15:06
  • 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.Acula – Offir Oct 31 '16 at 09:13
  • @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? – Romain G Nov 21 '16 at 11:56
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; 
        }
Kai
  • 5,850
  • 13
  • 43
  • 63
Venkata K. C. Tata
  • 5,539
  • 4
  • 22
  • 35
  • 2
    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? – Romain G Nov 21 '16 at 12:06
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/

meze
  • 14,975
  • 4
  • 47
  • 52
  • Thank you! This seems to be the better (and more robust) way to do it. Especially because it supports locales. – flu Oct 01 '14 at 10:36
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)
Sreekanth Karini
  • 1,265
  • 13
  • 14