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

Check if weekend exist in date range using javascript - Stack Overflow

programmeradmin3浏览0评论

Wondering if anyone has a solution for checking if a weekend exist between two dates and its range.

var date1 = 'Apr 10, 2014';
var date2 = 'Apr 14, 2014';


funck isWeekend(date1,date2){
   //do function

    return isWeekend;
}

Thank you in advance.

EDIT Adding what I've got so far. Check the two days.

function isWeekend(date1,date2){
   //do function
    if(date1.getDay() == 6 || date1.getDay() == 0){
        return isWeekend;
        console.log("weekend")
    } 
        if(date2.getDay() == 6 || date2.getDay() == 0){
        return isWeekend;
        console.log("weekend")
    } 
}

Wondering if anyone has a solution for checking if a weekend exist between two dates and its range.

var date1 = 'Apr 10, 2014';
var date2 = 'Apr 14, 2014';


funck isWeekend(date1,date2){
   //do function

    return isWeekend;
}

Thank you in advance.

EDIT Adding what I've got so far. Check the two days.

function isWeekend(date1,date2){
   //do function
    if(date1.getDay() == 6 || date1.getDay() == 0){
        return isWeekend;
        console.log("weekend")
    } 
        if(date2.getDay() == 6 || date2.getDay() == 0){
        return isWeekend;
        console.log("weekend")
    } 
}
Share Improve this question edited Jun 4, 2014 at 13:52 BaconJuice asked Jun 4, 2014 at 13:36 BaconJuiceBaconJuice 3,76915 gold badges59 silver badges90 bronze badges 6
  • 6 show us what you tried ? – Charaf JRA Commented Jun 4, 2014 at 13:38
  • Calculate the number of days between you start and end date, if it's more than 5, then it must contain a weekend day. (it's not clear if you want a whole weekend, or just a Saturday or a Sunday). If it's <5 days, then you need to use getDay() on the start and end date. This ranges from 0 for Sunday to 6 for Saturday, so again it should be easy to figure out if those days will fall in that range. – Matt Burland Commented Jun 4, 2014 at 13:49
  • There's an example here for you to check how too loop through 2 dates and in that loop you can check start.getDay(), value to determine if there's a weekend or not. – Batu.Khan Commented Jun 4, 2014 at 13:49
  • @BatuZet: I don't think you even need to loop. If you do getDay() on the start and the end, you should be able to work it out. – Matt Burland Commented Jun 4, 2014 at 13:50
  • Yeah @Matt Burland . I posted mine just after miliseconds you did. If i saw yours earlier, i wouldn't post mine :P – Batu.Khan Commented Jun 4, 2014 at 13:52
 |  Show 1 more comment

7 Answers 7

Reset to default 11

Easiest would be to just iterate over the dates and return if any of the days are 6 (Saturday) or 0 (Sunday)

Demo: http://jsfiddle.net/abhitalks/xtD5V/1/

Code:

function isWeekend(date1, date2) {
    var d1 = new Date(date1),
        d2 = new Date(date2), 
        isWeekend = false;

    while (d1 < d2) {
        var day = d1.getDay();
        isWeekend = (day === 6) || (day === 0); 
        if (isWeekend) { return true; } // return immediately if weekend found
        d1.setDate(d1.getDate() + 1);
    }
    return false;
}

If you want to check if the whole weekend exists between the two dates, then change the code slightly:

Demo 2: http://jsfiddle.net/abhitalks/xtD5V/2/

Code:

function isFullWeekend(date1, date2) {
    var d1 = new Date(date1),
        d2 = new Date(date2); 

    while (d1 < d2) {
        var day = d1.getDay();
        if ((day === 6) || (day === 0)) { 
            var nextDate = d1; // if one weekend is found, check the next date
            nextDate.setDate(d1.getDate() + 1); // set the next date
            var nextDay = nextDate.getDay(); // get the next day
            if ((nextDay === 6) || (nextDay === 0)) {
                return true; // if next day is also a weekend, return true
            }
        }
        d1.setDate(d1.getDate() + 1);
    }
    return false;
}

You are only checking if the first or second date is a weekend day.

Loop from the first to the second date, returning true only if one of the days in between falls on a weekend-day:

function isWeekend(date1,date2){
    var date1 = new Date(date1), date2 = new Date(date2);

    //Your second code snippet implies that you are passing date objects 
    //to the function, which differs from the first. If it's the second, 
    //just miss out creating new date objects.

    while(date1 < date2){
        var dayNo = date1.getDay();
        date1.setDate(date1.getDate()+1)
        if(!dayNo || dayNo == 6){
            return true;
        }
    }
}

JSFiddle

Here's what I'd suggest to test if a weekend day falls within the range of two dates (which I think is what you were asking):

function containsWeekend(d1, d2)
{
    // note: I'm assuming d2 is later than d1 and that both d1 and d2 are actually dates
    // you might want to add code to check those conditions
    var interval = (d2 - d1) / (1000 * 60 * 60 * 24); // convert to days
    if (interval > 5) {
        return true;    // must contain a weekend day
    }
    var day1 = d1.getDay();
    var day2 = d2.getDay();
    return !(day1 > 0 && day2 < 6 && day2 > day1);
}

fiddle

If you need to check if a whole weekend exists within the range, then it's only slightly more complicated.

It doesn't really make sense to pass in two dates, especially when they are 4 days apart. Here is one that only uses one day which makes much more sense IMHO:

var date1 = 'Apr 10, 2014';

function isWeekend(date1){
  var aDate1 = new Date(date1);
  var dayOfWeek = aDate1.getDay();

  return ((dayOfWeek == 0) || (dayOfWeek == 6));
}

I guess this is the one what @MattBurland sugested for doing it without a loop

function isWeekend(start,end){
  start = new Date(start);
  if (start.getDay() == 0 || start.getDay() == 6) return true;
  end = new Date(end);

  var day_diff = (end - start) / (1000 * 60 * 60 * 24);
  var end_day = start.getDay() + day_diff;    
  if (end_day > 5) return true;

  return false;
}

FIDDLE

Whithout loops, considering "sunday" first day of week (0):

Check the first date day of week, if is weekend day return true.

SUM "day of the week" of the first day of the range and the number of days in the lap. If sum>5 return true

Use Date.getDay() to tell if it is a weekend.

  if(tempDate.getDay()==6 || tempDate.getDay()==0)

Check this working sample:

http://jsfiddle.net/danyu/EKP6H/2/

This will list out all weekends in date span. Modify it to adapt to requirements. Good luck.

发布评论

评论列表(0)

  1. 暂无评论