javascript-Google API转换持续时间 [英] javascript - Google API converting duration

查看:63
本文介绍了javascript-Google API转换持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用Google API计算距离和持续时间,它可以很好地处理为字符串,但是我不想将这些数据存储为:

Currently I am using a google API to calculate distance and duration, and this works brilliantly into a string, however i don't want to store this data as:

2 hours 10 mins

是否有一种简便的方法可以在几分钟内转换此格式? (130)

我的逻辑是从字符串中提取数字,使其值等于210,然后对此进行一些数学计算:

my logic would be extract the numbers from the string so the value equals 210, and then perform some mathematical calculation on this:

var dur = screen.LabourAllocation.Duration;
var dura = dur.match(/\d/g);
dura = dura.join("");

还有谁能提出比这更好的方法?如果没有,我该怎么做才能将210转换为分钟(130)?

can anyone else suggest a better method than this? if not what calculation could i do to convert 210 to minutes (130)?

感谢您的帮助

推荐答案

您必须获取所有数字部分,您的正则表达式只能一一捕获数字,因此需要/\d+/g,然后需要简单的数学运算.

You have to get all number parts, your regex only catch numbers one by one so you need /\d+/g, then simple math.

var dur = "2 hours 10 mins";
var dura = dur.match(/\d+/g); // take all "grouped" number
// dura ["2", "10"]

dura = dura[0] * 60 + (+dura[1]); 
// first one is hours , second one need to be converted to number

这篇关于javascript-Google API转换持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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