我有一个如下的对象数组,
...
{"key_as_string": "14:11:00", //string type
"key": 1529579460000, // key_as_string date of type
"doc_count": 25
},
...
如何从HH:mm:ss
或key_as_string
转换日期类型key
?
答案 0 :(得分:1)
您可以尝试以下方法:
var timeFromJSON = JSONName.key_as_string;
var hours = timeFromJSON.split(':')[0];
var minutes = timeFromJSON.split(':')[1];
var seconds = timeFromJSON.split(':')[2];
var date = new Date(0, 0, 0, hours, minutes, seconds);
现在您可以执行以下操作:
date.getHours(); // Expected output: 14
date.getMinutes(); // Expected output: 11
date.getSeconds(); // Expected output: 0
答案 1 :(得分:0)
也许是这样的:
var long_date=1529579460000;
var date = new Date(long_date);
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
var out_date = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(out_date);