使用默认序列化器从WebAPI返回DateTime Json [英] DateTime Json Return From WebAPI with Default Serializer

查看:55
本文介绍了使用默认序列化器从WebAPI返回DateTime Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被多次哈希处理了,我读过很多关于哈希的文章,但仍然很困惑.

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

使用MVC4/WebAPI,我有一个简单创建为新DateTime.Now的日期时间.

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

我的WebAPI返回的数据如下:

My WebAPI is return data like this:

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

其中sessionRecordsSmall具有DateTime的公共属性.

where sessionRecordsSmall has a public property of DateTime in it.

当我在VS调试器中查看日期时,由于DateTime不包含时区,所以它显示为现在没有时区.

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}

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

当我查看从服务器下载的内容时,我会在JSON中看到

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

2012-10-06T09:45:00

2012-10-06T09:45:00

我认为T0表示时区0,而不是100%确定.我的JavaScript库将其解释为时区0,然后将实际下载的日期显示为GMT(对于我来说是-9个小时前).

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).

我的问题是,下载的JSON是什么?包括时区吗?我在这里错过了一些重要的步骤吗?

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

推荐答案

日期时间 2012-10-06T09:45:00 ,我们使用Web API在JSON中接收,默认序列化器为<一种href ="http://en.wikipedia.org/wiki/ISO_8601" rel ="nofollow noreferrer"> ISO 8601 格式.

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:

..单个时间点可以通过串联一个完整的日期表达式,字母T作为定界符,以及有效的时间表达.例如"2007-04-05T14:30" ...

..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:

ISO 8601中的时区表示为本地时间(带有未指定位置),UTC或UTC的偏移量.如果没有UTC关系信息以时间表示形式给出,时间是假定是在当地时间.

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.

换句话说,如果未指定与UTC的偏移量,则将其视为本地时间.

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

UTC格式将使用 Z 进行扩展最后

如果时间采用UTC,则在时间之后直接添加Z,而无需加空间.Z是零UTC偏移量的区域标记."09:30 UTC"因此表示为"09:30Z"或"0930Z"."14:45:15 UTC"将是"14:45:15Z"或"144515Z".

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时间也称为祖鲁语"时间,因为祖鲁语"是北约语音字母中的"Z".

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

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)

这篇关于使用默认序列化器从WebAPI返回DateTime Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆