How to calculate date difference in JavaScript?

前端 未结 18 1786
执笔经年
执笔经年 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 04:11

    I think this should do it.

    let today = new Date();
    let form_date=new Date('2019-10-23')
    let difference=form_date>today ? form_date-today : today-form_date
    let diff_days=Math.floor(difference/(1000*3600*24))
    

提交回复
热议问题