DayJS格式秒数分钟和小时

时间:2019-09-18 21:04:37

标签: javascript

当我从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');

但这没什么区别。

1 个答案:

答案 0 :(得分:1)

diff函数返回秒,分钟或小时的总数,而不是组成部分。

尝试一下:

branch_x    sessions    users   guests_x    branch_y    guests_y

您将获得三个具有所需值的变量: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 totalHourstotalMinutes