时间转换(秒数转时分秒,时间戳转年月日)

做一件事,无论大小,倘无恒心,是很不好的。而看一切太难,固然能使人无成,但若看得太容易,也能使事情无结果。-- 鲁迅《狂人日记》 

  • 将秒数转换成时分秒
// 将秒数转换成时分秒
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值