13

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?

4

2 回答 2

49

如果使用 json.net 进行序列化,请记住您可以指定DateTimeZoneHandling

WebApiConf.cs 中的示例

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.DateTimeZoneHandling =Newtonsoft.Json.DateTimeZoneHandling.Local;
于 2014-08-12T15:46:41.137 回答
15

2012-10-06T09:45:00我们使用 Web API 和默认序列化程序以 JSON格式接收的日期时间是ISO 8601格式。

事实上,这就是所谓的组合日期和时间表示。提炼:

..单个时间点可以通过连接完整的日期表达式、作为分隔符的字母 T 和有效的时间表达式来表示。例如“2007-04-05T14:30”...

这种格式没有时区信息。如时区指示符摘录中所述:

ISO 8601 中的时区表示为本地时间(未指定位置)、UTC 或与 UTC 的偏移量。如果没有给出带有时间表示的 UTC 关系信息,则假定时间为本地时间。

换言之,如果没有指定与 UTC 的偏移量,则将其视为本地时间。

UTC 格式Z在末尾扩展

如果时间为 UTC,则在时间后直接添加 Z,不带空格。Z 是零 UTC 偏移的区域指示符。因此,“09:30 UTC”表示为“09:30Z”或“0930Z”。“14:45:15 UTC”将是“14:45:15Z”或“144515Z”。

UTC 时间也称为“祖鲁”时间,因为“祖鲁”是“Z”的北约拼音字母词。

因此,我们收到的日期时间是 ISO 8601 格式,被视为本地时区Z最后不是这样2012-10-06T09:45:00Z

于 2013-05-22T17:49:40.690 回答