当我从hours minutes seconds获得diff值时,我想格式化dayjs。
这是我的代码:
newAppoint.occupied.push({
hours: dayjs().diff(user.appointment, 'hour'),
minutes: dayjs().diff(user.appointment, 'minute'),
seconds: dayjs().diff(user.appointment, 'second')
});现在的问题是,我得到了像0 hrs 3 min 228 sec一样的差异。
我怎样才能把它变成这样:00 hrs 03 min 59 sec
我尝试在push函数后面添加以下代码:
dayjs(newAppoint.occupied.hours).format('hh');
dayjs(newAppoint.occupied.minutes).format('mm');
dayjs(newAppoint.occupied.seconds).format('ss');但这并没有什么不同。
发布于 2019-09-19 05:23:58
diff函数返回总秒数、分钟数或小时数,而不是各组成部分。
试试这个:
totalSeconds = dayjs().diff(user.appointment, 'second');
totalHours = Math.floor(totalSeconds/(60*60)) // How many hours?
totalSeconds = totalSeconds - (totalHours*60*60) // Pull those hours out of totalSeconds
totalMinutes = Math.floor(totalSeconds/60) //With hours out this will retun minutes
totalSeconds = totalSeconds - (totalMinutes*60) // Again pull out of totalSeconds然后,您就有了三个具有所需值的变量:totalHours、totalMinutes和totalSeconds
https://stackoverflow.com/questions/58000703
复制相似问题