【发布时间】:2020-12-07 19:31:52
【问题描述】:
有没有办法使用moment.js 将var timeValue = '65' 格式化为01:05?它更容易格式化为 ('HH:MM:SS') 但将变量传递为 65 并转换为 ('mm:ss') 给我的响应是 '01:00' 而不是 '01:05',
查看
<div id="app">
<p>
Time is {{roomTime}}
</p>
<br/>
<p>
How to make it display 01:05 not 01:00
</p>
</div>
脚本
new Vue({
el: "#app",
data() {
return{
roomTime: '',
}
},
mounted: function(){
var timeValue = '65'; /** As 65 is 1 minute and 5 seconds **/
var formatTime = moment(timeValue).format("MM:SS");
/** How to make 65 to be display as 01:05 **/
this.roomTime = formatTime;
},
methods: {
toggle: function(todo){
todo.done = !todo.done
}
}
})
下面是我在JSFIDDLE
中的代码参考【问题讨论】:
-
Moment 有一个durations 的概念,可用于此目的。不幸的是,format a duration 的功能已经消失了八年或更长时间。有一个 github issue 请求格式化功能,它有一些替代方法和解决方法。
标签: javascript vue.js momentjs