search

Home  >  Q&A  >  body text

How to calculate date difference in JavaScript

<p>I want to calculate the difference between dates in days, hours, minutes, seconds, milliseconds and nanoseconds. How do I do this? </p>
P粉252116587P粉252116587461 days ago470

reply all(1)I'll reply

  • P粉239164234

    P粉2391642342023-08-23 00:28:13

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

    var difference = date2 - date1;

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

    reply
    0
  • Cancelreply