在JavaScript,jQuery,moment.js或任何库中,
如何更改数字值,例如 116265 (11小时,62分钟,65秒)
进入正确的时间值 12:03:05 ?
修改/更新
如果小时数大于2位,它也必须工作。 (在我的情况下,小时数是唯一可以大于2位数的部分。)
答案 0 :(得分:4)
将 replace()
方法与功能
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
})
)

答案 1 :(得分:1)
试试这个
var a = '116265'.replace(/^(\d+)(\d{2})(\d{2})$/, '$1:$2:$3')
var hms = a; // your input string
var a = hms.split(':'); // split it at the colons
if (a[2] >= 60) {
a[2] -= 60, a[1] ++;
}
if (a[1] >= 60) {
a[1] -= 60, a[0] ++;
}
console.log(a[0] + ":" + ("0" + a[1]).slice(-2) + ":" + ("0" + a[2]).slice(-2));