I want to calculate date difference in days, hours, minutes, seconds, milliseconds, nanoseconds, how can I do it? Please suggest.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Assuming you have two
Date
objects, you can just subtract them to get the difference in milliseconds:From there, you can use simple arithmetic to derive the other values.
If you are using moment.js then it is pretty simple to find date difference.
Sorry but flat millisecond calculation is not reliable Thanks for all the responses, but few of the functions I tried are failing either on 1. A date near today's date 2. A date in 1970 or 3. A date in a leap year.
Approach that best worked for me and covers all scenario e.g. leap year, near date in 1970, feb 29 etc.
you can also use this very easy to use date manipulation library http://www.datejs.com/
ex:
(Works very well with date formatting libraries)