www.webdeveloper.com
Results 1 to 5 of 5

Thread: simple <> comparison of dates

  1. #1
    Join Date
    Aug 2005
    Posts
    243

    simple <> comparison of dates

    I have two form fields with dates in the mm-dd-yyyy format. I need to validate that date2 > date1. How might I make this comparison?

  2. #2
    Join Date
    Apr 2006
    Location
    Houston
    Posts
    1,374
    Code:
    if (new Date (date2) > new Date (date1)) {
    ...Do Something...
    }

  3. #3
    Join Date
    Jun 2006
    Location
    Wisconsin
    Posts
    58
    Code:
    d1="12-18-2009";
    d2="12-15-2009";
    date1=new Date(d1.split("-")[2], d1.split("-")[0], d1.split("-")[1]);
    date2=new Date(d2.split("-")[2], d2.split("-")[0], d2.split("-")[1]);
    
    if(date1<date2){do something}

  4. #4
    Join Date
    Aug 2005
    Posts
    243
    Thanks for the feedback...here's what i finally did to get it to work...

    Code:
    var startDate = $('#start_date').val().replace('-','/');
    var endDate = $('#end_date').val().replace('-','/');
    
    if(startDate > endDate){
       // do stuff here...
    }
    I'm using jQuery, hence the $(..)... syntax

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443

    Lightbulb Something to consider ...

    This does not use a JQuery solution, but consider this:
    Code:
    function cvrtDate(d) {
      var t = d.split(/\D+/)	// splits on any character(s) between digits
      var dt = new Date();
      dt.setFullYear(t[2],(t[0]-1),t[1]);
      return dt;
    }
    Advantage is that the argument passed to the cvrtDate function can be any of the following formats
    '1/1/2010'
    '1-1-2010'
    '1.1.2010'
    or even
    '1/1-2010' or any other non-digit separator.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles