Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime Json Return From WebAPI with Default Serializer

I know this question has been hashed over multiple times and I read lots of posts on that hashing but still am confused.

Using MVC4/WebAPI, I have a datetime that is simply created as new DateTime.Now.

My WebAPI is return data like this:

 HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new
            {
                data = sessionRecordSmalls,
                count = sessionRecordSmalls.Count,
                success = true
            });

where sessionRecordsSmall has a public property of DateTime in it.

When I look at the date in the VS debugger, it shows it as now with no timezone of course because DateTime does not include a timezone.

{10/6/2012 9:45:00 AM}

When I look at what gets downloaded from the server, I see in the JSON

2012-10-06T09:45:00

I think the T0 means Timezone 0, not 100% sure of that. My JavaScript library interprets it as timezone 0, then shows the actual date downloaded as GMT (-9 hours ago for me).

My question is, what is the JSON downloaded? Is that include a timezone? Am I missing some important step here?

like image 823
Peter Kellner Avatar asked May 22 '13 16:05

Peter Kellner


People also ask

What is JSON serialization in Web API?

Serialization and deserialization in . NET application, JSON data format conversion to . NET objects and vice versa is very common. Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.

Which library does Web API use for JSON Serialisation?

JSON Media-Type Formatter By default, JsonMediaTypeFormatter uses the Json.NET library to perform serialization. Json.NET is a third-party open source project.


1 Answers

The date time 2012-10-06T09:45:00, which we recive in JSON with Web API and default serializer is the ISO 8601 format.

In fact this is so called Combined date and time representations. Extract:

..A single point in time can be represented by concatenating a complete date expression, the letter T as a delimiter, and a valid time expression. For example "2007-04-05T14:30"...

There is no time zone information in this format. As mentioned in the Time zone designators Extract:

Time zones in ISO 8601 are represented as local time (with the location unspecified), as UTC, or as an offset from UTC. If no UTC relation information is given with a time representation, the time is assumed to be in local time.

In other words, if there is no offset from UTC specified, it is treated as a local time.

The UTC format would be extended with Z at the end

If the time is in UTC, add a Z directly after the time without a space. Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or "144515Z".

UTC time is also known as 'Zulu' time, since 'Zulu' is the NATO phonetic alphabet word for 'Z'.

So, the date-time we recieve is the ISO 8601 format, treated as local time zone (no Z at the end like this 2012-10-06T09:45:00Z)

like image 144
Radim Köhler Avatar answered Oct 11 '22 06:10

Radim Köhler