Compare date to a list of dates?
wondering if there is an easy way to do this..
I have a list of dates (like 3 or 4) as strings.. I need to check if currDate is == to any of them
If (currDate=="02/13/2016" || currDate=="04/12/2016" etc etc
I could split and loop... but seems eh
edit... yes they are strings... so... Date.parse?
edit.. no wait.. Date.parse returns a number.. so no lol
Re: Compare date to a list of dates?
You did not mention what language this is so I'm going to assume that it is JavaScript.
Take a look at this example:
Code:
var dates = ["02/11/2016", "02/12/2016", "02/13/2016"];
var today = new Date();
today.setHours(0,0,0,0);
for(var dateIndex = 0; dateIndex < dates.length; dateIndex++) {
var iteratedDate = new Date(dates[dateIndex]);
iteratedDate.setHours(0,0,0,0);
if (today.getTime() === iteratedDate.getTime()) {
alert('matched at index ' + dateIndex.toString());
}
}
Re: Compare date to a list of dates?
What format are your date strings in? Better to have them in ISO8601 (YYYY-MM-DD) format if possible so there's no confusion for the browser.
Moment.js also has some nice stuff for manipulating dates in JavaScript.