一、moment.js在vue中使用
npm install moment
//或者
yarn add moment
// require 方式
var moment = require('moment');
// import 方式
import moment from 'moment';
//使用
moment().format()
具体使用请查看官方文档
首页 | Moment.js 中文文档Moment.js 是一个用于解析、校验、操作、显示日期和时间的 JavaScript 工具库。https://momentjs.bootcss.com/
二、具体将时间秒转化小时、分钟、秒格式方法
<script>
export default {
data(){},
methods: {
getSleepTime(t){
if (t > 0) {
const date = moment.duration(t,'seconds')
const hours = Math.floor(date._data.hours);
const minutes = Math.floor(date._data.minutes);
const seconds = Math.floor(date._data.seconds);
return (hours > 0 ? ` ${hours}小时` : '') +
(minutes > 0 ? ` ${minutes}分钟` : '') +
(seconds > 0 ? ` ${seconds}秒` : '')
} else {
return 0
}
},
}
}
</script>