最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Compare Current Date and Time to input of type DateTime - Stack Overflow

matteradmin4PV0评论

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 pare 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.

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 pare 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.

Share Improve this question asked Nov 29, 2013 at 12:30 KapteinMarshallKapteinMarshall 4906 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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.

Post a comment

comment list (0)

  1. No comments so far