Posted By

MMDeveloper on 10/17/08


Tagged

javascript date validation


Versions (?)

Date Validation


 / Published in: JavaScript
 

Example of concept on how to do date calculations in javascript.

  1. First convert the date parts to a UTC representation
  2.  
  3. firstDate = Date.UTC(year, month, day, hours, minutes, seconds);
  4. secondDate = Date.UTC(year, month, day, hours, minutes, seconds);
  5.  
  6. now, if(firstDate < secondDate)
  7.  
  8. then firstDate is an earlier date. The Date.UTC method converts the date to a timestamp,
  9. the number of seconds since midnight of January 1, 1970 according to universal time. So you can do standard math comparisions.
  10.  
  11.  
  12. To calculate the difference between the 2 dates in days.
  13.  
  14. day = 60*60*24; //60 seconds, times 60 minutes, times 24 hours.
  15.  
  16. differenceInDays = Math.abs(secondDate - firstDate) / day;
  17.  
  18. The subtraction will return the difference between the 2 dates in seconds. It will then take the absolute value of that,
  19. and divide it by the number of seconds in a day, resulting in the number of days between the 2 dates.

Report this snippet  

You need to login to post a comment.