使用jquery和moment js获得下星期开始和结束。

时间:2022-05-05 07:12:23

I searched for this question and found there is a no answer on *.. 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.

如果你需要从下周一开始到下周结束,这个问题会很有帮助。

3 个解决方案

#1


110  

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 8601标准,isoWeek参数从周一开始,而根据您的语言环境(可能是周日或周一),每周开始。

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.

我喜欢这些结构,因为它们非常易读。通过指定相减或相加需要的周数,也可以很容易地返回或向前移动任意周数。

#2


10  

I used moment js for this ... u can get it from here

我用了moment 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; 
        }

#3


5  

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:

这是在lang文件中指定的,您可以包括lang/en-au。js或朗/ en。并设置所需的语言标准。假设你在英国:

moment.lang('en-gb');

If you don't want to use a custom language, you can change it for the default US locale:

如果您不想使用自定义语言,可以将其更改为默认的US语言环境:

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/

#1


110  

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 8601标准,isoWeek参数从周一开始,而根据您的语言环境(可能是周日或周一),每周开始。

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.

我喜欢这些结构,因为它们非常易读。通过指定相减或相加需要的周数,也可以很容易地返回或向前移动任意周数。

#2


10  

I used moment js for this ... u can get it from here

我用了moment 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; 
        }

#3


5  

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:

这是在lang文件中指定的,您可以包括lang/en-au。js或朗/ en。并设置所需的语言标准。假设你在英国:

moment.lang('en-gb');

If you don't want to use a custom language, you can change it for the default US locale:

如果您不想使用自定义语言,可以将其更改为默认的US语言环境:

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/