27

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?

sudoer
  • 195
  • 12
Patrioticcow
  • 26,422
  • 75
  • 217
  • 337

6 Answers6

63
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/

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
12
alert(new Date(1273185387).toUTCString());
Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
9

Try the below code...

    var myDate = new Date( your epoch date *1000);
    alert(myDate.toGMTString());
    var mytime=myDate.toGMTString()
Sangeet Menon
  • 9,555
  • 8
  • 40
  • 58
5

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
4

Convert json date to date format in jQuery

 <script>
  var date = "\/Date(1297246301973)\/";
  var nowDate = new Date(parseInt(date.substr(6)));
  alert(nowDate )
</script>
Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
3

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());
pepkin88
  • 2,742
  • 20
  • 19