Question

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.

Était-ce utile?

La solution 2

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

Autres conseils

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.

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/

    //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)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top