如何使用moment.js将时间(hh:mm)转换为十进制形式 [英] How to convert time (hh:mm) to decimal form with momentjs

查看:411
本文介绍了如何使用moment.js将时间(hh:mm)转换为十进制形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用momentjs将小时分钟转换为十进制时间的最佳方法是什么

what is the best way to convert Hours Minutes to Decimal Time with momentjs

假设我有1:30分钟,并且我希望在转换后达到1.5

let say i have 1:30 min and i want to have 1.5 after the conversion

这是我尝试过的

console.log(travelTime().split(':')[0]);   return 1
console.log(travelTime().split(':')[1]);   return 30  then / 30

其中travelTime()=="1:30"

where travelTime() == "1:30"

推荐答案

看起来您想要的是moment.duration(durationAsString)解析持续时间,而.asHours()则格式化为小数.

It looks like what you want is moment.duration(durationAsString) to parse your duration and .asHours() to format as decimal.

moment.duration('1:30').asHours();将返回1.5.

有关更多信息,请参见有关持续时间的Moment文档部分: http://momentjs.com/docs/#/durations/

For more information, see Moment's documentation section on durations: http://momentjs.com/docs/#/durations/

这篇关于如何使用moment.js将时间(hh:mm)转换为十进制形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆