最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

check if selected dates are from the same week javascript - Stack Overflow

programmeradmin1浏览0评论

How to find the given dates are from the same week using javascript?

like if i give dates like, the week starts SUNDAY

the below dates fall between (12 July 2015 to 18 July 2015)

07/13/2015 
07/14/2015 
07/15/2015 
07/16/2015 
07/20/2015 

in the above list 20 does not follow on the week and it falls on the next week.

would some one help me on this?

I also would like to find the same check for to see if the given dates are same date like

07/13/2015 
07/13/2015 
07/13/2015
07/13/2015 
07/13/2015 
07/13/2015 
07/14/2015 

in the above all are same date except 14. so please help me to find if the given dates are same date.

I am okay to use external lib also

How to find the given dates are from the same week using javascript?

like if i give dates like, the week starts SUNDAY

the below dates fall between (12 July 2015 to 18 July 2015)

07/13/2015 
07/14/2015 
07/15/2015 
07/16/2015 
07/20/2015 

in the above list 20 does not follow on the week and it falls on the next week.

would some one help me on this?

I also would like to find the same check for to see if the given dates are same date like

07/13/2015 
07/13/2015 
07/13/2015
07/13/2015 
07/13/2015 
07/13/2015 
07/14/2015 

in the above all are same date except 14. so please help me to find if the given dates are same date.

I am okay to use external lib also
Share Improve this question edited Jul 15, 2015 at 5:40 Joe asked Jul 15, 2015 at 5:18 JoeJoe 4,54219 gold badges61 silver badges107 bronze badges 4
  • who defines the start of the week – Arun P Johny Commented Jul 15, 2015 at 5:19
  • I guess for US it starts from SUNDAY , pls correct me if i am wrong – Joe Commented Jul 15, 2015 at 5:23
  • I mean.. the date.... whether to consider 12 as the start or 19th – Arun P Johny Commented Jul 15, 2015 at 5:24
  • Oh, sorry got it , have to define this from the first date given. – Joe Commented Jul 15, 2015 at 5:29
Add a ment  | 

2 Answers 2

Reset to default 3

A simpler solution would be using using a library like dayjs which is only 2kb.

In dayjs you can use the isSame() function

Example:

dayjs().isSame('2021-10-09', 'week')

Note: week starts from Sunday. Don't forget to make necessary changes if you want Monday as the day 1 of the week.

If the week always starts on Sunday, you could check if they are all in the same week by doing something like this:

function getMinAndMax(dates) {
    var result = {};
    for (var index in dates) {
        var thisDate = dates[index]
        ,   dateParts = thisDate.split(/\//)
        ,   fullDate = new Date(dateParts[2], dateParts[0] - 1, dateParts[1]);
        if(!result['max'] || fullDate > result['max']) {
            result['max'] = fullDate;
        }
        if(!result['min'] || fullDate < result['min']) {
            result['min'] = fullDate
        }
    }
    return result;
}
function isSameWeek(dates) {
    var minAndMax = getMinAndMax(dates)
    ,   dayOfWeek = {}
    dayOfWeek['min'] = minAndMax['min'].getDay();
    dayOfWeek['max'] = minAndMax['max'].getDay();
    if(minAndMax['max'] - minAndMax['min'] > 518400000 || dayOfWeek['min'] > dayOfWeek['max']) {
        return false;
    }
    return true;
}

To use the above function, you would do:

var datesArray = [
    '07/13/2015'
,   '07/14/2015'
,   '07/15/2015'
,   '07/16/2015'
,   '07/10/2015'
];
isSameWeek(datesArray); // false

var datesArray2 = [
    '07/15/2015'
,   '07/14/2015'
,   '07/15/2015'
,   '07/15/2015'
,   '07/13/2015'
];
isSameWeek(datesArray2); // true

To see if all the dates are the same, you could include the getMinAndMax function declared earlier and then do:

function areAllDatesSame(dates) {
    var minAndMax = getMinAndMax(dates);
    return minAndMax['min'] == minAndMax['max']
}

You could call it like this:

var datesArray = [
    '07/13/2015'
,   '07/14/2015'
,   '07/15/2015'
,   '07/16/2015'
,   '07/13/2015'
];
areAllDatesSame(datesArray); // false

var datesArray2 = [
    '07/13/2015'
,   '07/13/2015'
,   '07/13/2015'
,   '07/13/2015'
,   '07/13/2015'
];
areAllDatesSame(datesArray2); // true
发布评论

评论列表(0)

  1. 暂无评论