-2

I have tried the following code to compare two dates

    //the following check validates that the date is entered in the right format and it works
    if(s_period=="" || !date_format.exec(s_period))
    {
            alert("Please enter start date as YYYY-MM-DD");
            return false;
    }

    //next, i want to check if s_period is greater than '2013-06-30'
     if((new Date(s_period).getTime()) < (new Date("2013-07-01").getTime()))
     {
            alert("Stat available from 2013-07-01");
            return false;

      }

The above alert for choosing a date after 2013-06-30 gets invoked in Firefox but, NOT on IE8. Can someone suggest the additional code I need to add to make it work across all browser version? Thanks

Shuvo Shams
  • 633
  • 4
  • 10
  • 22
  • 1
    Possible duplicate http://stackoverflow.com/questions/4944750/how-to-subtract-date-time-in-javascript –  Sep 02 '14 at 19:47
  • Is this your entire program? – James Sep 02 '14 at 19:48
  • What is date_form.exec? – aquinas Sep 02 '14 at 19:48
  • date_format=/^(\d{4})\-(\d{2})\-(\d{2})$/. Compares the date against the regular expression date_format – Shuvo Shams Sep 02 '14 at 19:49
  • 1
    Works for me: http://jsfiddle.net/ughcnnom/ – aquinas Sep 02 '14 at 19:53
  • It appears the .getTime() or new Date(s_period_.getTime() is not working. Because, if I include an else statement after the last if to alert s_period, I do get the right user input value. Am I missing any java library for Date function to work?? – Shuvo Shams Sep 03 '14 at 13:15
  • Alright, it is actually an issue with IE8 as it works fine on firefox. Any suggestion on what changes are needed in the code to make it compatible with IE8? – Shuvo Shams Sep 03 '14 at 16:33

1 Answers1

1

Try Date.parse http://msdn.microsoft.com/en-us/library/ie/k4w173wk(v=vs.94).aspx

e.g.

if((Date.parse(s_period)) < (Date.parse("2013-07-01")))
NoGray
  • 1,149
  • 7
  • 9
  • Thanks for showing me that function. However, it still does not work on IE8. The function works on Firefox b/c I tried the following: d_s_period = Date.parse("2013-01-01"); document.write(d_s_period); It prints NaN in IE 8 but, 1356998400000 on Firefox – Shuvo Shams Sep 03 '14 at 18:16
  • Change the date format to '2013/01/01' (use / instead of -) – NoGray Sep 03 '14 at 18:44