js通过Moment.js让数值秒值和时分秒格式相互转化

本文介绍了如何利用JavaScript库Moment.js将时分秒格式的时间转换为数值型秒值,以及如何将数值型秒值转换回时分秒格式。示例代码详细展示了转换过程,对于前端开发中处理时间格式的转换非常实用。
摘要由CSDN通过智能技术生成

js通过Moment.js让数值秒值和时分秒格式相互转化

1、把时分秒格式转化为数值型的秒值

<script src="https://momentjs.com/downloads/moment.js"></script>
<script>
	let time=moment.duration('01:01:01').as('seconds')
	console.log(time)  //3661
</script>

2、把数值型的秒值转化为时分秒格式

<script src="https://momentjs.com/downloads/moment.js"></script>
<script>
  let time = moment.duration(3661, 'seconds')  //得到一个对象,里面有对应的时分秒等时间对象值
  let hours = time.hours() 
  let minutes = time.minutes()
  let seconds = time.seconds()
  console.log(moment({h:hours, m:minutes, s:seconds}).format('HH:mm:ss'))
  //01:01:01
</script>
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值