时刻utc从一天开始不正确

时间:2017-11-10 17:41:06

标签: javascript date momentjs

自那个具体日期以来,我正试着在几分钟之前给我一些时间。我以UTC时间格式发送日期。 DD MMM YYYY的第一种格式,h:mm a是正确的。但是,前一段时间都是35,这是不正确的。

Sat Nov 11 2017 00:13:45 GMT+0700 (+07)
11 Nov 2017, 12:13 am
35 minutes ago
Sat Nov 11 2017 00:10:46 GMT+0700 (+07)
11 Nov 2017, 12:10 am 
35 minutes ago
Sat Nov 11 2017 00:12:08 GMT+0700 (+07)
11 Nov 2017, 12:10 am 
35 minutes ago
Sat Nov 11 2017 00:07:57 GMT+0700 (+07)
11 Nov 2017, 12:07 am
35 minutes ago

代码:

    console.log(date)
    console.log(moment(date).local().format('DD MMM YYYY, h:mm a'))
    if(today.diff(date, 'days') < 5){
        date = moment(date).local().startOf('day').fromNow()
        console.log(date)            
    }else{
        date = moment(date).local().format('DD MMM YYYY, h:mm a')
    }

1 个答案:

答案 0 :(得分:1)

改为使用diff,然后将单位指定为分钟。

let date = '2017/02/22 08:42:22';
let diff = moment(date).diff(moment(date).startOf('day'), 'minutes');

您也可以

let date = '2017/02/22 08:42:22';
let diff = moment(date).diff(moment(date).startOf('day'));

以毫秒为单位返回差值,然后通过

显示
moment.duration(diff).asMinutes();
moment.duration(diff).asHours();

如果你想混合单位,你可以这样做:

`${Math.floor(moment.duration(diff).asHours())}:${moment.duration(diff).minutes()}`

Documentation

编辑:oops,忘记了正确的语法。这就是我从内存中写出来的东西