2

I have googled a bit and could not find an answer. So here is my situation.

I have an input of type dateTime. I want to compare the value picked (mobile app for blackberry) to the current date and time. if the selected date is in the future (bigger than date now) I have to show a simple error message. This is all done when the user tries to save the data.

I have tried code like this, but was unsucessfull.

 var dateOfIncident = $('#AccidentDetailsDate').val();
 var dateNow = Date.now();

 if(dateNow > dateOfIncident)
 { 
       // do my stuffs :)
 }  

This does not work... It passes that validation. I am very new to javascript myself. Any help would be greatly appreciated. I googled and could not find a solution that does not use anything fancy. I need to do it in javascript.

Thanks in advance.

KapteinMarshall
  • 490
  • 6
  • 20

1 Answers1

5

Try this:

var dateOfIncident = new Date($('#AccidentDetailsDate').val()); // or Date.parse(...)
var dateNow = new Date(); // or Date.now()
if(dateNow > dateOfIncident)
{
    // do your stuffs...
}

However, if this works may depend on what format your date-string is! You may want to consider this post as well.

Community
  • 1
  • 1
marsze
  • 15,079
  • 5
  • 45
  • 61
  • Wow this works perfectly!!! Thanks. I tried parsing dates a few which ways around, unsuccessfully.. This works very well. Thanks. – KapteinMarshall Nov 29 '13 at 12:44