.net JSON序列化程序将本地客户端时间返回到浏览器? [英] .net JSON serializer returns local client time to browser?

查看:124
本文介绍了.net JSON序列化程序将本地客户端时间返回到浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net [WebMethod]将.net对象推回到浏览器上的Ajax调用. 对象的属性之一是DateTime类型.

当它到达浏览器时,时间比SQL Server中存储的时间早了七个小时.

好的,我的浏览器在秘鲁(GMT-5),服务器在德国(当前是GMT + 2),这就是7个小时的发源地.

I use an asp.net [WebMethod] to push a .net object back to the Ajax call on a browser. One of the properties of the object is of a DateTime type.

When it arrives at the browser the time is seven hours before the time that is stored in the SQL Server.

Okay, so my browser is in Peru (GMT-5) and the server is in Germany (currently GMT+2), that's where the 7 hours come from.

作为修复,我通过Ajax请求在客户端上发送了UTC偏移量

As a fix I send the UTC offset on the client with the Ajax request

d = new Date();
d.getTimezoneOffset();

然后在服务器上,我找出那里的偏移量:

then on the server I figure out the offset there:

  // get a local time zone info
  TimeZoneInfo tz = TimeZoneInfo.Local;

  // get it in hours
  int offset = tz.BaseUtcOffset.Hours;

  // add one hour if we are in daylight savings
  if (tz.IsDaylightSavingTime(DateTime.Now))
  {
      offset++;
  }

现在,我可以先将对象中的时间字段发送给浏览器.

Now I can fix the time field in my object before it is send to the browser.

我真正的问题是,序列化程序如何知道这7个小时?

My real question is, how does the serializer know about the 7 hours?

http请求不包含任何时间信息.

The http request doesn't include any time information.

我问的太多了吗,我是否想要数据库中存储的准确时间?

Do I ask too much if I want the exact time as stored in the database?

更新:

以下是示例,数据库中的日期为:2009年10月15日22:00

Here's an example, the date in the database is: 2009-Oct-15 22:00

没有附加的时区信息.

当我在客户机和服务器所在的开发机器上调用WebMethod时 显然在同一时区,来自服务器的JSON是:

When I call my WebMethod on my dev machine where client and server are obviously in the same time zone, the JSON from the server is:

{"d":{"TheDate":"\/Date(1255662000000)\/"}}

来自德国远程服务器的JSON是:

The JSON from the remote server in Germany is:

{"d":{"TheDate":"\/Date(1255636800000)\/"}}

在Firebug中,JSON的区别是7个小时.在这 指向尚无JavaScript.

There is a difference of 7 hours in the JSON as seen in Firebug. At this point there is no JavaScript involved yet.

我的一个想法是,asp.net将TimeZone附加到会话上,但事实并非如此.

One idea I had is that asp.net attaches a TimeZone to a session but that doesn't seem to be the case.

推荐答案

为回答OP问题,时区信息在转换为JSON/Date()/格式时是隐式的,因为它相对于UTC.例如,在我位于纽约的服务器上,如果我返回DateTime.Parse("1/1/1970"),它将返回/Date(18000000)/或5小时(我们现在处于DST中),是自1/1/1970 UTC 以来的秒数,因为转换显示为嘿,在纽约,这里是1/1/1970 00:00:00,所以它必须是1/1 /70 05:00:00返回格林威治."

To answer the OPs question, the timezone information is implicit in the conversion to the JSON /Date()/ format, because it is relative to UTC. For example, on my server here in NY, if I return a DateTime.Parse("1/1/1970"), it returns /Date(18000000)/, or, 5 hours (we're in DST now), which is the number of seconds since 1/1/1970 UTC, since the conversion says, "hey, it's 1/1/1970 00:00:00 here in NY, so it must be 1/1/70 05:00:00 back over in Greenwich."

现在,如果加利福尼亚的一位客户收到此日期符号,并仅以毫秒为单位实例化一个JavaScript日期(例如,新的Date(18000000)),浏览器将说:嘿,这是一个日期对象,我知道是相对于UTC的,我知道我距格林威治(Greenwich)8个小时,所以它必须是12/31/1969 21:00:00."

Now, if a client in California received this date notation, and simply instantiates a JavaScript date from the milliseconds (e.g. new Date(18000000)), the browser will say, "hey, here is a date object, which I know is relative to UTC, and I know I am 8 hours from Greenwich, so it must be 12/31/1969 21:00:00."

因此,这是一种处理时间的非常聪明的方法,因此它在所有时区都是正确的",并且所有本地化都由用户的浏览器处理.不幸的是,我们经常只处理一个原始日期,我们不想成为相对于时区的日期(例如生日).如果我们需要保持相同的日期,我知道有两种方法.

So this is a pretty clever way to deal with time, so that it is "correct" in all time zones, and such that all localization is handled by the user's browser. Unfortunately, we are often dealing with just a raw date that we don't want to be time zone relative (say, a birthday). If we need to keep the date the same, there are two ways that I know of.

首先,就像您在上面所做的那样,是调整时间(尽管我认为您也需要在浏览器中进行调整,如果您希望它可以在任何时区工作).

The first, as you have done above, is to adjust the time (although I think you need to do it at the browser, too, if you want it to work in any time zone).

另一种方法是将其作为已格式化的字符串返回.这是我通常采用的方法,但是我通常与美国客户一起工作(例如,我可以返回MM/DD/YYYY,而他们不会因为成为美国人而生我的气).

The other way would be to return it as a string, formatted already. This is the method I normally employ, but I am normally working with US clients (so I can return MM/DD/YYYY, for example, and they don't get mad at me for being American).

这篇关于.net JSON序列化程序将本地客户端时间返回到浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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