自那个具体日期以来,我正试着在几分钟之前给我一些时间。我以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')
}
答案 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()}`
编辑:oops,忘记了正确的语法。这就是我从内存中写出来的东西