datejavascript将数值转换为时间值 [英] datejavascript change a numeric value into time value
本文介绍了datejavascript将数值转换为时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在JavaScript,jQuery,moment.js或任何库中,
In JavaScript, jQuery, moment.js or any library,
您如何更改像 116265 这样的数值(11小时62分65秒)
how would u change a numeric value like 116265 (11 hours, 62 minutes, 65 seconds)
输入正确的时间值 12:03:05 吗?
编辑/更新:
如果小时数大于2位,它也必须工作. (就我而言,小时是唯一可以大于2位的部分.)
It must work also if the hours are bigger then 2 digits. (In my case the hours are the only part that can be bigger then 2 digits.)
推荐答案
console.log(
'116265'.replace(/^(\d+)(\d{2})(\d{2})$/, function(m, m1, m2, m3) {
m1 = Number(m1); // convert captured group value to number
m2 = Number(m2);
m2 = Number(m2);
m2 += parseInt(m3 / 60, 10); // get minutes from second and add it to minute
m3 = m3 % 60; // get soconds
m1 += parseInt(m2 / 60, 10); // get minutes from minute and add it to hour
m2 = m2 % 60; // get minutes
// add 0 to minute and second if single digit , slice(-2) will select last 2 digit
return m1 + ':' + ('0' + m2).slice(-2) + ':' + ('0' + m3).slice(-2); // return updated string
})
)
这篇关于datejavascript将数值转换为时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文