简体   繁体   English

使用JavaScript检查周末是否在日期范围内

[英]Check if weekend exist in date range using javascript

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")
    } 
}

Easiest would be to just iterate over the dates and return if any of the days are 6 (Saturday) or 0 (Sunday) 最简单的方法是迭代日期,然后在任意一天为6 (星期六)或0 (星期日)时返回

Demo: http://jsfiddle.net/abhitalks/xtD5V/1/ 演示: 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/ 演示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: 从第一个日期到第二个日期循环播放,仅当介于两者之间的某一天是周末时才返回true:

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 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. 传递两个日期实际上没有任何意义,尤其是当它们相隔4天时。 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 我想这就是@MattBurland提出的无循环操作

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): Whithout循环,考虑一周的第一天(0):

Check the first date day of week, if is weekend day return true. 检查星期的第一个日期,如果是周末则返回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 如果sum> 5返回true

Use Date.getDay() to tell if it is a weekend. 使用Date.getDay()判断是否是周末。

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

Check this working sample: 检查此工作示例:

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

This will list out all weekends in date span. 这将列出日期范围内的所有周末。 Modify it to adapt to requirements. 对其进行修改以适应需求。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM