14

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月06日T09: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?

asked May 22, 2013 at 16:47

2 Answers 2

50

if serializing with json.net keep in mind that you can specify DateTimeZoneHandling.

Example in WebApiConf.cs

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.DateTimeZoneHandling =Newtonsoft.Json.DateTimeZoneHandling.Local;
answered Aug 12, 2014 at 15:46
0
16

The date time 2012年10月06日T09: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月06日T09:45:00Z)

wtjones
4,1614 gold badges38 silver badges42 bronze badges
answered May 22, 2013 at 17:49
2
  • 3
    I notice that when I decorate my datetime with: [JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter))] public DateTime LogDate { get; set; } I get what I expect with the conversion, should that not be the default? Commented May 22, 2013 at 20:29
  • Honestly not sure ... but I would suggest to read this icanmakethiswork.blogspot.cz/2012/04/… ... I am doing conversion into UTC also with some hacking Commented May 23, 2013 at 4:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.