0
var data = {
    "duration": {
        "value": 74384,
        "text": "20 hours 40 mins" //convert this to a valid time date format
      },
      "distance": {
        "value": 2137146,
        "text": "1,328 mi"
      },
}

Done few sample test:

var date = new Date(Date.parse(data.duration.text));
var date = moment(new Date(data.duration.text));

reference: https://developers.google.com/maps/documentation/directions/intro#UnitSystems

Roman
  • 4,922
  • 3
  • 22
  • 31
Harish98
  • 445
  • 1
  • 7
  • 16
  • 2
    How exactly you think this would work ? Javascript date is time in milliseconds since 1.1.1970 if you want to make a new date from `20 hours 40 mins` the time will be `1.1.1970. 20 hours 40 mins` ... – anteAdamovic Nov 10 '17 at 07:18
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse – Edison Augusthy Nov 10 '17 at 07:22

3 Answers3

4

As per the documentation, the duration property has two attributes : text and value.

The value attribute returns the time in seconds. You can add in current time.

var data = {
     "duration": {
     "value": 74384, //in seconds
     "text": "20 hours 40 mins"
   },
   "distance": {
     "value": 2137146,
     "text": "1,328 mi"
   },
};

var seconds = data.duration.value;//from value
var finalDateInMiliSeconds = new Date().getTime()+(seconds*1000);
console.log(new Date(finalDateInMiliSeconds));

Now, you can convert it any format you want. Refer Convert date to specific format in javascript?

Darshan Patel
  • 2,839
  • 2
  • 25
  • 38
2

var data= {
"duration": {
        "value": 74384,
        "text": "20 hours 40 mins" //convert this to a valid time date format
      },
      "distance": {
        "value": 2137146,
        "text": "1,328 mi"
      },
};

let date = new Date();
date = new Date(date.getFullYear(),date.getMonth(),date.getDate());
let whatYouWant = new Date(+date + data.duration.value*1000);
document.getElementById('time').innerText = whatYouWant;
<span id='time'></span>
J.Du
  • 271
  • 3
  • 4
1

Try this code for time conversion.

function secondsToHms(d) {
    d = Number(d);
    var h = Math.floor(d / 3600);
    var m = Math.floor(d % 3600 / 60);
    var s = Math.floor(d % 3600 % 60);

    var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
    var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
    var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
    return hDisplay + mDisplay + sDisplay; 
}

refrence of code is:

A.D.
  • 2,352
  • 2
  • 15
  • 25