How to find the difference between two dates?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By using the Date object and its milliseconds value, differences can be calculated:
You can get the number of seconds (as a integer/whole number) by dividing the milliseconds by 1000 to convert it to seconds then converting the result to an integer (this removes the fractional part representing the milliseconds):
You could then get whole
minutesby dividingsecondsby 60 and converting it to an integer, thenhoursby dividingminutesby 60 and converting it to an integer, then longer time units in the same way. From this, a function to get the maximum whole amount of a time unit in the value of a lower unit and the remainder lower unit can be created:If you’re wondering what the input parameters provided above for the second Date object are, see their names below:
As noted in the comments of this solution, you don’t necessarily need to provide all these values unless they’re necessary for the date you wish to represent.