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
5
  • 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. Commented Mar 6, 2012 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. Commented Mar 6, 2012 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? Commented Mar 6, 2012 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. Commented Mar 6, 2012 at 16:52
  • See this post for the easiest answer stackoverflow.com/questions/206384/how-to-format-a-json-date Commented Jul 3, 2012 at 14:03

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

1 Comment

This post shows the easiest way to get the date object from the string. stackoverflow.com/questions/206384/how-to-format-a-json-date
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

Comments

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];

Comments

0

Format the date like this:

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

and:

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.