/ Published in: JavaScript
Example of concept on how to do date calculations in javascript.
Expand |
Embed | Plain Text
First convert the date parts to a UTC representation firstDate = Date.UTC(year, month, day, hours, minutes, seconds); secondDate = Date.UTC(year, month, day, hours, minutes, seconds); now, if(firstDate < secondDate) then firstDate is an earlier date. The Date.UTC method converts the date to a timestamp, the number of seconds since midnight of January 1, 1970 according to universal time. So you can do standard math comparisions. To calculate the difference between the 2 dates in days. day = 60*60*24; //60 seconds, times 60 minutes, times 24 hours. differenceInDays = Math.abs(secondDate - firstDate) / day; The subtraction will return the difference between the 2 dates in seconds. It will then take the absolute value of that, and divide it by the number of seconds in a day, resulting in the number of days between the 2 dates.
You need to login to post a comment.
