How to calculate date difference in JavaScript?

前端 未结 18 1992
执笔经年
执笔经年 2020-11-22 03:41

I want to calculate date difference in days, hours, minutes, seconds, milliseconds, nanoseconds. How can I do it?

18条回答
  •  無奈伤痛
    2020-11-22 03:48

    Assuming you have two Date objects, you can just subtract them to get the difference in milliseconds:

    var difference = date2 - date1;
    

    From there, you can use simple arithmetic to derive the other values.

提交回复
热议问题