0

For some reason when I do a ToString("d") on a date in C# and that's then serialized in JSON to the client, when I try to render that value to the page, I get this literal text instead of the date formatted:

/Date(-62135575200000)/

I can't figure out what's going on here.

PositiveGuy
  • 46,620
  • 110
  • 305
  • 471

3 Answers3

0

You can try this - which formats the date and display as mm/dd/yyyy format. I was facing the same issue which i solved by using this :

var regxformatdate = /-?\d+/;
var integerformat = regxformatdate.exec(msg.d);
var dt = new Date(parseInt(integerformat));
var newdate = dt.getMonth()+1+ "/" + dt.getDate()+"/" + dt.getFullYear();
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
0

Neither JSON nor JavaScript have date literals, so there are various conventions. That's Microsoft's, which even they describe as "contrived".

You can see this question about ways to decode it.

Community
  • 1
  • 1
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
0

You will need to convert the value if you are passing dates back and forth. In javascript,

var d = new Date()
d.setTime(-62135575200000);
alert(d.toDateString());

See the question Converting .NET DateTime to JSON and related answers there.

The following shows the two ways commented upon to move the dates. in my code behind:

 [WebMethod]
 public static DateTime loadDate()
 {
     return DateTime.Now;
 }
 [WebMethod]
 public static double loadDateTicks()
 {
     return DateTime.Now.UnixTicks();
 }
 public static class ExtensionMethods
 {
    // returns the number of milliseconds since Jan 1, 1970 (useful for converting C# dates to JS dates)
    public static double UnixTicks(this DateTime dt)
    {
        DateTime d1 = new DateTime(1970, 1, 1);
        DateTime d2 = dt.ToUniversalTime();
        TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
        return ts.TotalMilliseconds;
    }
 }

All credit for that extension method goes to "Jeff Meatball Yang".

My front end test is the following:

function LoadDates() {
        $.ajax({
            url: "Default.aspx/loadDate",
            type: "POST",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            success: function (msg) {
                var re = /-?\d+/;
                var d = new Date(parseInt(re.exec(msg.d)[0]));
                alert(d.toDateString());
            },
            dataType: "json"
        });
        $.ajax({
            url: "Default.aspx/loadDateTicks",
            type: "POST",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            success: function (msg) {
                var dt = new Date(msg.d);
                alert(dt.toDateString());
            },
            dataType: "json"
        });
    }
Community
  • 1
  • 1
Chris Carew
  • 1,398
  • 10
  • 6
  • 1
    As an addendum, the comment a little ways down on that thread has a method of converting a datetime to a double. This is a useful convention. My experience has been largely that, if I need to send dates back and forth, ticks work well; and if it's purely for display, strings are occasionally easiest/most appropriate. – Chris Carew Mar 15 '12 at 22:51
  • when I am able to get it into some format I get Sat Dec 31 3938 which is obviously not right. – PositiveGuy Mar 15 '12 at 23:11
  • What are you sending from .net? What are you using to parse it? – Chris Carew Mar 15 '12 at 23:20
  • we've been able to just use ToString("d") to send over to JS via json and it's worked, I just can't get mine to work....not sure why. – PositiveGuy Mar 16 '12 at 00:15
  • it's not a problem with the JS receiving code, that's been working fine for other types of dates. – PositiveGuy Mar 16 '12 at 00:16
  • We may need a little more info on exactly what date you're sending, and how, and how you're receiving it. When you say the JS has been working for other types of dates, do you mean from .Net dates, or another language/construct? – Chris Carew Mar 16 '12 at 16:39