Get the time difference between two datetimes

前端 未结 19 1962
花落未央
花落未央 2020-11-22 05:25

I know I can do anything and some more envolving Dates with momentjs. But embarrassingly, I\'m having a hard time trying to do something that seems simple: geting the differ

19条回答
  •  孤街浪徒
    2020-11-22 05:55

    EPOCH TIME DIFFERENCE USING MOMENTJS:

    To Get Difference between two epoch times:

    Syntax: moment.duration(moment(moment(date1).diff(moment(date2)))).asHours()

    Difference in Hours: moment.duration(moment(moment(1590597744551).diff(moment(1590597909877)))).asHours()

    Difference in minutes: moment.duration(moment(moment(1590597744551).diff(moment(1590597909877)))).asMinutes().toFixed()

    Note: You could remove .toFixed() if you need precise values.

    Code:

    const moment = require('moment')
    
    console.log('Date 1',moment(1590597909877).toISOString())
    console.log('Date 2',moment(1590597744551).toISOString())
    console.log('Date1 - Date 2 time diffrence is : ',moment.duration(moment(moment(1590597909877).diff(moment(1590597744551)))).asMinutes().toFixed()+' minutes')
    
    

    Refer working example here: https://repl.it/repls/MoccasinDearDimension

提交回复
热议问题