问题
This question already has an answer here:
- Convert UTC Epoch to local date 15 answers
i have a json file that returns "date_created":"1273185387"
in epoch format
i want to convert it to something like this Thu, 06 May 2010 22:36:27 GMT
any script to do this conversion?
回答1:
var myObj = $.parseJSON('{"date_created":"1273185387"}'),
myDate = new Date(1000*myObj.date_created);
console.log(myDate.toString());
console.log(myDate.toLocaleString());
console.log(myDate.toUTCString());
http://jsfiddle.net/mattball/8gvkk/
回答2:
Try the below code...
var myDate = new Date( your epoch date *1000);
alert(myDate.toGMTString());
var mytime=myDate.toGMTString()
回答3:
alert(new Date(1273185387).toUTCString());
回答4:
jQuery doesn't have anything for it, but that's okay, because JavaScript does. The Date
constructor accepts a milliseconds-since-the-Epoch value, so in your case (since that looks like a seconds value) it would be:
var dt = new Date(obj.date_created * 1000);
...where obj
is the result of deserializing that JSON string.
Details in Section 15.9.3.2 of the specification. Alternately, the MDC page is useful.
回答5:
http://jsfiddle.net/y3Syc/1/
var data = {"date_created":"1273185387"};
var date = new Date(parseInt(data.date_created, 10) * 1000);
// example representations
alert(date);
alert(date.toLocaleString());
回答6:
Convert json date to date format in jQuery
<script>
var date = "\/Date(1297246301973)\/";
var nowDate = new Date(parseInt(date.substr(6)));
alert(nowDate )
</script>
来源:https://stackoverflow.com/questions/5722919/jquery-convert-number-to-date