get next week start and end using jquery and moment js

前端 未结 4 1283
灰色年华
灰色年华 2020-12-08 02:47

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 n

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 03:27

    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/

提交回复
热议问题