Results 1 to 3 of 3

Thread: Compare date to a list of dates?

  1. #1

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    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.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width