Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查日期是否介于两个日期之间_Javascript_Jquery - Fatal编程技术网

Javascript 检查日期是否介于两个日期之间

Javascript 检查日期是否介于两个日期之间,javascript,jquery,Javascript,Jquery,我有一个具有不同日期的数组,格式为年-月-日 大概是这样的: var dates = ["2016-08-01", "2016-08-09", "2016-08-10", ....]; 我还有一个函数,可以将今天的日期格式化为与上面相同的格式。并存储在变量中: var currentDate; //Contains current date in the format of year-month-day 我需要做的是检查数组中是否有任何日期: 与今天的日期匹配。-例如,今天将是2016-0

我有一个具有不同日期的数组,格式为年-月-日

大概是这样的:

var dates = ["2016-08-01", "2016-08-09", "2016-08-10", ....];
我还有一个函数,可以将今天的日期格式化为与上面相同的格式。并存储在变量中:

var currentDate; //Contains current date in the format of year-month-day
我需要做的是检查数组中是否有任何日期:

  • 与今天的日期匹配。-例如,今天将是2016-08-13
  • 从今天起14天内进行匹配。-e、 g.从今天(2016-08-13)起14天将是2016-07-30
  • 或与当前日期和14天前的任何日期匹配
我试图通过循环数组,检查每个值来实现这一点。但是我不确定if条件

    for(var i = 0; i < dates.length; i++) {
      if(currentDate === *condition*) {
        sendDate(dates[i]);
      } 
    }
for(变量i=0;i

有人有好的解决办法吗?提前谢谢

一种方法是将日期解析为毫秒值并进行比较

var todayParts = currentDate.split('-');
var today = new Date(todayParts[0], todayParts[1], todayParts[2]).getTime();
var otherParts = dates[i].split('-');
var other = new Date(otherParts[0], otherParts[1], otherParts[2]).getTime();

if (today < other + 1209600000 /* 2 weeks in milliseconds */) {
    // The other date is less than 2 weeks before today
}
var todayParts=currentDate.split('-');
var today=新日期(todayParts[0]、todayParts[1]、todayParts[2])。getTime();
var otherParts=日期[i]。拆分('-');
var other=新日期(otherParts[0]、otherParts[1]、otherParts[2])。getTime();
如果(今天<其他+1209600000/*2周,以毫秒为单位*/){
//另一个日期距离今天不到两周
}
您可以阅读我为什么手动解析它,而不是使用
Date.parse()

首先,从当前日期创建一个新日期()(当前日期是字符串格式Y-d-m,带小时,分钟是00:00)

只需在日期数组中循环

for (var i=0; i<dates.length; i++) {

    var tmpDate = new Date(dates[i]); //Convert string to date
    var diff = Math.ceil((current - tmpDate) / (1000 * 3600 * 24)); //get time difference (current - tmpDate is milisecond, we need convert it to day)

    // Check condition and push it on array correct

    if (diff == 0) {
        matchWithCurrent.push(dates[i]);
    }

    if (diff == 14) {
        matchWithDayBack.push(dates[i]);
    }

    if ((diff > 0) && (diff <14)) {
        between.push(dates[i]);
    }

}

console.log(matchWithCurrent);
console.log(matchWithDayBack);
console.log(between);
对于(var i=0;i0)&&(diff
您可以比较两个类似的日期:
var currentDate=新日期();
对于(var i=0;i
var arrDate=新日期(日期[i]);
如果(currentDate==arrDate)
{//将数组日期与当前日期进行比较
//你的代码在这里
}
var beforeDate=(currentDate.getDate()-14);//14天前

如果(arrDate>=beforeDate&&arrDate,您似乎根本不关心当前日期,您只关心今天之前/之后的14天。从变量中包含这些值开始,并使用标准的大于/小于运算符。您的意思是什么?当前日期显然很重要,因为它每天都不同。
for (var i=0; i<dates.length; i++) {

    var tmpDate = new Date(dates[i]); //Convert string to date
    var diff = Math.ceil((current - tmpDate) / (1000 * 3600 * 24)); //get time difference (current - tmpDate is milisecond, we need convert it to day)

    // Check condition and push it on array correct

    if (diff == 0) {
        matchWithCurrent.push(dates[i]);
    }

    if (diff == 14) {
        matchWithDayBack.push(dates[i]);
    }

    if ((diff > 0) && (diff <14)) {
        between.push(dates[i]);
    }

}

console.log(matchWithCurrent);
console.log(matchWithDayBack);
console.log(between);
You can compare two dates something like that:
var currentDate = new Date();
 for(var i = 0; i < dates.length; i++) {<br>
 var arrDate = new Date(dates[i]);
    if(currentDate == arrDate)
    {//compare array dates with current date
        //your code here
    }
 var beforeDate = (currentDate.getDate() - 14); //14 days before
    if(arrDate >= beforeDate && arrDate <= currentDate){
        //compare dates between current date and 14 days before date
    } 
  }