gpt4 book ai didi

javascript - DayJS格式秒分时

转载 作者:行者123 更新时间:2023-11-30 19:16:00 34 4
gpt4 key购买 nike

当我从 dayjs 获取 diff 值时,我想格式化 hours minutes seconds

这是我的代码:

    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');

但这没有任何区别。

最佳答案

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 totalMinutestotalSeconds

关于javascript - DayJS格式秒分时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58000703/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com