How to handle JSON dates returned by ASP.NET AJAX?

The problem of handling dates in JSON is one of the more complex problems that can arise when directly calling ASP.NET AJAX web services and page methods. Unlike any other data type, JavaScript does not offer a declarative method for expressing dates. Hence, embedding them in JSON requires a little fancy work.

I will try to explain what exactly is the problem with dates in JSON.

What is the problem?

The main problem is that JavaScript does not provide a way to declaratively express Date objects. You may have seen earlier how the (lack of) Date literal is described. What are literals? To illustrate this, these are literals for several other data types:

// String
'foo';

// Number
3.14;

// Boolean
true;

// Array
[1, 2, 3, 5, 7];

// Object
{ pi: 3.14, phi: 1.62 };

      

Unfortunately, when it comes to dates, the lack of a literal means the only way to create it is to explicitly initialize the Date object:

// Correct.
new Date('4/26/09');

// Correct (the month is 0 indexed, hence the 3).
new Date(2009, 3, 26);

// Incorrect. This is a string, not a Date.
'4/26/09';

      

While this limitation is fine for writing client-side JavaScript code, it leaves us unable to pass dates in JSON objects. The lack of a date literal is a problem, can anyone suggest a solution.

+3


source to share


3 answers


Consider why you want to send a DateTime to the client side to begin with. More often than not, you are showing a string representation and do not need a corresponding JavaScript Date object. Moreover, if you have a JavaScript Date object, you are probably using additional code or JavaScript library to display it in a user-friendly format. As much as I appreciate the clever workaround, I'd much rather fix the problem entirely. Rather than going through all these hoops to instantiate a client-side JavaScript Date object and then format it, I suggest simply returning the formatted string. For example,

[System.Web.Script.Services.ScriptService]
public class DateService : System.Web.Services.WebService
{
[WebMethod]
public string GetDate()
{
return new DateTime(2009, 4, 26).ToLongDateString();
}
}

      

Now the service call will return this JSON:



{"d":"Sunday, April 26, 2009"}

      

More regular expressions. More JavaScript date objects. No more worrying about client-side data formatting. Moreover, functionality is not lost. If we need to create a date, we can still.

+1


source


I recently wrote a blog post about this species here ... it's a minefield! At work, we decided to pass DateTimes and DateTimeOffsets from our MVC3 service as strings, using the W3C-formatted options found here . For DateTime we use YYYY-MM-DDThh: mm: ss, for DateTimeOffsets we use YYYY-MM-DDThh: mm: ss + TZD (these format specifiers are W3C, not .NET).



But the answer to this question really depends on what you want to do with the dates. As the other answer suggests, you can get away by simply sending a human readable string over the wire if all you want to do is show it to the end user.

+1


source


I use two ways, one is to do Date.UTC () and the other is to make it like Ticks. They both work.

A function that converts it to Json Ticks

  private static readonly long UnixEpochTicks = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks;

  public static long cOnlyDate(long Ticks)
  {
     return (864000000000 * (Ticks / 864000000000));
  }

  public static long ToJsonTicks(DateTime value)
  {
    return (cOnlyDate(value.ToUniversalTime().Ticks) - UnixEpochTicks) / 10000;
  }

      

Results will be similar to {dt : 28839281}

and how do i convert it to Date

public static string ToJsonDate(DateTime value)
{
    return string.Format("Date.UTC({0}, {1}, {2})", value.Year, value.Month-1, value.Day);
}

      

Results will be similar to {d : Date.UTC(2012, 2, 11)}

ps, I'm just testing the "Unknow" solution and not working on my code.

0


source







All Articles