|
-
Feb 11th, 2016, 12:49 PM
#1
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
Last edited by Static; Feb 11th, 2016 at 01:04 PM.
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 11th, 2016, 04:01 PM
#2
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());
}
}
Last edited by dday9; Feb 12th, 2016 at 09:23 AM.
Reason: Fixed indenting of code
-
Feb 11th, 2016, 06:20 PM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|