将纪元日期转换为有意义的 Javascript 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11565540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Epoch Date to meaningful Javascript date
提问by Ravi
I am getting a string value "/Date(1342709595000)/"
in the JSON. I am trying to extract the digits alone and convert the epoch date to meaning ful Javascript Date in the format mm/dd/yy hh:mm:ss . I was able to achieve the first part of the question extracting the digits but couldnot convert it to date object human readable format as available in http://www.epochconverter.com/
我"/Date(1342709595000)/"
在 JSON 中得到一个字符串值。我试图单独提取数字并将纪元日期转换为格式为 mm/dd/yy hh:mm:ss 的有意义的 Javascript 日期。我能够实现问题的第一部分提取数字,但无法将其转换为http://www.epochconverter.com/ 中提供的日期对象人类可读格式
JS Fiddle: http://jsfiddle.net/meetravi/QzKwE/3/
回答by Esailija
There is nothing you really need to do, they are already milliseconds since epoch and javascript dates take milliseconds since epoch.
没有什么你真的需要做的,它们已经是自纪元以来的毫秒数,而 javascript 日期则是自纪元以来的毫秒数。
var dateVal ="/Date(1342709595000)/";
var date = new Date(parseFloat(dateVal.substr(6)));
document.write(
(date.getMonth() + 1) + "/" +
date.getDate() + "/" +
date.getFullYear() + " " +
date.getHours() + ":" +
date.getMinutes() + ":" +
date.getSeconds()
);
?
?