片刻js格式化时间不正常

时间:2014-11-03 12:45:28

标签: jquery momentjs

我有疑问,如何将'90:33' - 'mm:ss'格式化为'01:30:33' - 'hh:mm:ss'? 片刻js不格式化90分钟,因为它超过60,结果是'无效日期' 我试过这个:

console.log(moment('90:33', ['mm:ss', 'hh:mm:ss']).format('hh:mm:ss'));

1 个答案:

答案 0 :(得分:1)

如你所说,90作为分钟参数无效。我会做这样的事情:

// Assuming duration is a string in the format "mm:ss" e.g. "90:33"
var parts = duration.split(":");
var minutes = duration[0];
var seconds = duration[1];


var d = moment.duration({minutes: minutes, seconds: seconds});
var as_string = d.hours() + ":" + d.minutes() + ":" + d.seconds(); // "1:30:33"