根据日期数组检查日期 [英] check date against an array of dates

查看:66
本文介绍了根据日期数组检查日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,该函数根据日期是否存在于数组中来返回TRUE或FALSE值.目前我有这个:

  function isInArray(value,array){var a = array.indexOf(value)>-1;如果(a == false){返回false;//日期不存在}别的 {返回true;//日期存在于数组中}} 

现在通常我会使用for循环,但是我使用while循环生成了开始日期和结束日期之间的日期列表:

  while(day> 0){var tDate = new Date(sDate.addDays(dayCounter));dateArray.push(tDate);天=天-1;//推算添加的日子var dateExists = isInArray(value,array);如果(dateExists ==否){}别的 {matchDays ++;}daysCounter ++;} 

但是这不起作用并且总是返回FALSE,我该怎么做才能使它工作并返回正确的值?

样本数据: ARRAY [26/12/2016,27/12/2016,28/12/2016,29/12/2016]

[27/12/2016]

中传递的

  • 如果该值存在于数组中,则返回TRUE
  • 如果该值在数组中不存在,则返回FALSE

上面可能是一个简单的错误!因此,谢谢您对此的任何帮助.时间也会影响检查日期吗?

解决方案

您可以尝试使用


情况:数组包含Date对象

I am trying to write a function that returns a TRUE or FALSE value based on whether the date is present in the array or not. Currently I have this:

function isInArray(value, array) {
    var a = array.indexOf(value) > -1;

    if (a == false) {
        return false; //DATE DOES NOT EXIST
    }
    else {
        return true; //DATE EXISTS IN ARRAY
    }
}

Now normally I would use a for loop, however I am generating a list of dates between a start date and end date with this while loop:

 while (day > 0) {
     var tDate = new Date(sDate.addDays(dayCounter));
     datesArray.push(tDate);
     day = day - 1; //DEDUCT THE DAYS AS ADDED

     var dateExists = isInArray(value, array);
     if (dateExists == false) {

     }
     else { 
         matchedDays++;
     }
     daysCounter++;
}

this however is not working and always returns FALSE, what do I need to do to make this work and return the correct value?

sample data: ARRAY[26/12/2016, 27/12/2016, 28/12/2016, 29/12/2016]

value passed in [27/12/2016]

  • return TRUE if the value exists in the array
  • return FALSE if the value does not exist in the array

probably a simple mistake above! so thankyou for any help on this. also will a time affect the date when its being checked?

解决方案

You can try using the find() method (if ecmascript is supported):

Case: array contains strings

var array = ["26/12/2016", "27/12/2016", "28/12/2016", "29/12/2016"];
var value1 = "26/12/2016";  //exists
var value2 = "26/12/2026";  //doesn't exist

function isInArray(array, value) {
  return (array.find(item => {return item == value}) || []).length > 0;
}

console.log(isInArray(array, value1));
console.log(isInArray(array, value2));


Case: array contains Date objects

var array = [new Date("December 26, 2016 00:00:00"), new Date("December 27, 2016 00:00:00"), new Date("December 28, 2016 00:00:00"), new Date("December 29, 2016 00:00:00")];
var value1 = new Date("December 26, 2016 00:00:00");  //exists
var value2 = new Date("December 16, 2026 00:00:00");  //doesn't exist


function isInArray(array, value) {
  return !!array.find(item => {return item.getTime() == value.getTime()});
}

console.log(isInArray(array, value1));
console.log(isInArray(array, value2));

这篇关于根据日期数组检查日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆