By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,257 Members | 1,569 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,257 IT Pros & Developers. It's quick & easy.

How to compare dates in javascript

100+
P: 250
I want to compare date in java script for validation. This is my code.
Expand|Select|Wrap|Line Numbers
  1.         var currentDate = new Date()
  2.      var day = currentDate.getDate()
  3.       var month = currentDate.getMonth()
  4.       var year = currentDate.getFullYear()
  5.     var today = (year + "-" + month + "-" + day);
  6. ...
  7. else if (ED > today){
  8.     err= 'End Date cannot be grater than the current date!';
  9.  
There are two text boxes to enter date. one is start date and the other one is end date. When user enter end date which is grater than start date I want to give error message. But I'll get error message when end date is current date. Where I goes wrong?

Ex: Start Date :2009-11-30
End Date: 2009-12-01
if the current date is 2009-12-02. I have got error message.
Dec 2 '09 #1
Share this Question
Share on Google+
1 Reply


RamananKalirajan
100+
P: 607
Expand|Select|Wrap|Line Numbers
  1. var newDate = new Date(y,m,d);
  2. var today = new Date();
  3. dif = newDate-today;//difference in milliseconds
  4. dif = Math.ceil(dif/1000/60/60/24);//difference in days
  5. f.difD.value = dif + ' days';//assign the difference 
This code will give you the exact day difference between two dates... The y, m, d in the first line of the code are the user entered date, month and year.

Thanks and Regards
Ramanan Kalirajan
Dec 2 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.