做一件事,无论大小,倘无恒心,是很不好的。而看一切太难,固然能使人无成,但若看得太容易,也能使事情无结果。-- 鲁迅《狂人日记》
- 将秒数转换成时分秒
// 将秒数转换成时分秒
formatSeconds(value) {
var result = parseInt(value)
var h =
Math.floor(result / 3600) < 10
? '0' + Math.floor(result / 3600)
: Math.floor(result / 3600)
var m =
Math.floor((result / 60) % 60) < 10
? '0' + Math.floor((result / 60) % 60)
: Math.floor((result / 60) % 60)
var s =
Math.floor(result % 60) < 10
? '0' + Math.floor(result % 60)
: Math.floor(result % 60)
var res = ''
if (h !== '00') res += `${h}:`
if (m !== '00') res += `${m}:`
res += `${s}`
return res
}
console.log(this.formatSeconds(6000)); // 01:40:00
- 时间戳转年月日
onlyFormat(timeStamp) {
var date = new Date(timeStamp) // 时间戳为10位需*1000,时间戳为13位的话不需乘1000
var Y = date.getFullYear() + '-'
var M =
(date.getMonth() + 1 < 10
? '0' + (date.getMonth() + 1)
: date.getMonth() + 1) + '-'
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
// var h =
// (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
// var m =
// (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
// ':'
// var s =
// date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
return Y + M + D // 时分秒可以根据自己的需求加上
},
console.log(this.onlyFormat(1589990077899)); // 2020-05-20