javascript - Google API converting duration

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

is there an easy way to convert this format in minutes? (130)

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("");

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

thanks for any help

Answers:

Answer

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.