Skip to content Skip to sidebar Skip to footer

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 the

Solution 1:

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

Post a Comment for "Javascript - Google Api Converting Duration"