0

I have date data being returned from a server in the following format through JSON

/Date(1139443200000)/

If I try to display this on a web browser using the following code snippet:

success: function( rows ) {
    if( rows.d[0] ) {
        $.each( rows.d, function( index, row ) {
            $("div").append( row.OpenDate );

the date gets displayed on the screen like this:

/Date(1139443200000)/

How do I format this to display like this

09-02-2006

and

09-02-2006 - 00:00
oshirowanen
  • 15,297
  • 82
  • 198
  • 350
  • 3
    With respect, I'm pretty sure you're mistaken about how the date looks in the JSON. If you look at the data returned for the call in a debugger, I expect you'll find it looks like this: `OpenDate: "/Date(1139443200000)/"`. That's a fairly common way to send dates in JSON text, as JSON (incomprehensibly) has no concept of dates. – T.J. Crowder Mar 06 '12 at 16:38
  • @T.J.: I don't know, dates are a nightmare in any encoding, so the spec was a lot simpler without them. Might as well just pass the seconds-since-epoch value around, since it is the least ambiguous. – Phil H Mar 06 '12 at 16:41
  • @T.J. Crowder, You're right, the data is `"OpenDate":"\/Date(1139443200000)\/"` even in JSON. So how do I format that to display as required? – oshirowanen Mar 06 '12 at 16:42
  • @PhilH: I probably shouldn't have said "incomprehensibly." :-) I actually do comprehend why he didn't do it: He wanted something that was a subset of JavaScript literal notation, and JavaScript literal notation's only date handling at that time would have been via `new Date(number)`, where the number would be milliseconds since The Epoch UTC (or it could be `new Date(year[, month[,...]])`). And that was probably too language-specific. It's too bad, a **lot** of hassle could have been prevented by defining a simply-parsed subset of ISO-8601 with values being in UTC. – T.J. Crowder Mar 06 '12 at 16:52
  • See this post for the easiest answer http://stackoverflow.com/questions/206384/how-to-format-a-json-date – David Mårtensson Jul 03 '12 at 14:03

5 Answers5

2

this can help you :

  var dateString = 1139443200000;   
    var myDate = new Date(dateString);

    document.write("Day of Weak: "+(myDate.getDay()+1));
    document.write("<br>");
    document.write("Month : " + (myDate.getMonth()+1));
    document.write("<br>");
    document.write("Year : " + myDate.getFullYear()); 

output :

Day of Weak: 5
Month : 2
Year : 2006
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • This post shows the easiest way to get the date object from the string. http://stackoverflow.com/questions/206384/how-to-format-a-json-date – David Mårtensson Jul 03 '12 at 14:03
1

You can use this as a reference to format your date http://blog.stevenlevithan.com/archives/date-time-format

or

var now = new Date();

dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM
Kamran Ali
  • 5,904
  • 2
  • 26
  • 36
0

I guess you need to manually parse the serialized Date string, e.g. using a regex:

var match = string.match(/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/);
var output = match[3] + "-" + match[2] + "-" + match[1] + " - " + match[4] + ":" + match[5];
Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
0

Format the date like this:

row.OpenDate.format("dd-mm-yy");

and:

row.OpenDate.format("dd-mm-yy - hh:MM");
Rian Schmits
  • 3,096
  • 3
  • 28
  • 44
0

A generic reusable approach

I've written a jQuery extension (not a plugin) that makes it possible to auto convert Asp.net date strings (as well as ISO ones) into actual Javascript instances when doing $.parseJSON. You can then do whatever you like with javascript dates.

jQuery parseJSON automatic date conversion for Asp.net and ISO date strings

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404