在javascript中将文本时间值转换为有效的日期格式

时间:2017-11-10 07:10:40

标签: javascript angularjs momentjs simpledateformat

arg0.getTag();// Type cast to your object type;

完成少量样本测试:

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"
      },
}

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

3 个答案:

答案 0 :(得分:4)

根据documentation,duration属性有两个属性: textvalue

value属性返回seconds中的时间。您可以添加当前时间。



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



    

现在,您可以将其转换为您想要的任何格式。请参阅Convert date to specific format in javascript?

答案 1 :(得分: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>

答案 2 :(得分:1)

尝试使用此代码进行时间转换。

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: