javascript - DayJS 格式秒分钟和小时
问题描述
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');
但这没有任何区别。
解决方案
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
推荐阅读
- javascript - 使用reduce时出现此错误“未捕获的类型错误:a不可迭代”
- javascript - 如何在 PHP / Javascript 中的 cookie 过期前一分钟提醒用户?
- c# - 使用没有通用参数的表达式树创建对象
- android - google_maps_flutter 在某些 Android 模拟器和设备上显示空白地图
- amazon-web-services - 域名服务器不变
- django - 如何让 Django ModelForm 接受特定格式的日期(或转换为可接受的格式)?
- html - 在反应js中提交表单后设置状态
- flutter - 模仿 iOS 联系表单 AppBar
- git - Git 从分支推送某些提交
- sapui5 - sap.ui.getCore().createComponent 因 sapui5 版本 1.72 而失败