当本地计算机和服务器处于不同时区时,Json 返回不同的日期
我在 json 日期解析中遇到一个奇怪的问题。我使用以下内容来解析 json 日期:
dateFormat(new Date(parseInt(user.RegDate.substr(6))), "mm/dd/yyyy")
当我的本地计算机(客户端)与服务器时区处于不同时区时,当我尝试检索用户的注册日期时,它会返回不同的日期。
例如:
SQL 中的注册日期:2010-07-22 19:00:00.000
当我在 IST 时区的本地计算机中调试时,JsonResult 返回的日期是:
/Date(1279805400000)/
Thu Jul 22 19:00:00 UTC+0530 2010
与我时的数据相同从采用 EST 时区的已部署服务器访问它,返回的 JsonResult 日期为:
/Date(1279843200000)/
Fri Jul 23 05:30:00 UTC+0530 2010
当我将本地计算机更改为 EST 时区时,此功能完美(返回相同日期 - 7 月 22 日星期四)。我在这里错过了什么吗?请建议
服务器代码是[编辑]:
public JsonResult GetregisteredUsersJSON()
{
var usersList = this.GetregisteredUsers()
return Json(usersList, JsonRequestBehavior.AllowGet);
}
private List<Users> GetregisteredUsers()
{
return (from u in _context.mu_Users
orderby u.Reg_Date descending
select new Users
{
FirstName = u.First_Name,
LastName = u.Last_Name,
RegDate = u.Reg_Date
}).ToList();
}
I have a strange problem in json date parsing. I am using the following to parse the json date:
dateFormat(new Date(parseInt(user.RegDate.substr(6))), "mm/dd/yyyy")
When my local machine (Client) is in different timezone from the server timezone, then it returns different dates when i try to retrieve the registered date of the users.
For ex.:
Registered date in SQL: 2010-07-22 19:00:00.000
When i debug in local machine which is in IST Timezone, the dates from JsonResult returned are:
/Date(1279805400000)/
Thu Jul 22 19:00:00 UTC+0530 2010
The same data when i access it from the deployed server which is in EST timezone, the dates from JsonResult returned are:
/Date(1279843200000)/
Fri Jul 23 05:30:00 UTC+0530 2010
This works perfect (returns same date - Thu Jul 22) when i change my local machine to EST Timezone. Am i missing anything here?. Please suggest
The Server Code is [EDIT]:
public JsonResult GetregisteredUsersJSON()
{
var usersList = this.GetregisteredUsers()
return Json(usersList, JsonRequestBehavior.AllowGet);
}
private List<Users> GetregisteredUsers()
{
return (from u in _context.mu_Users
orderby u.Reg_Date descending
select new Users
{
FirstName = u.First_Name,
LastName = u.Last_Name,
RegDate = u.Reg_Date
}).ToList();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果将数据保存在数据库中而不指定它是 UTC,那么您将希望阻止 JsonResult 在序列化期间转换 DateTime。为此,您需要使用 ActionResult 而不是 JsonResult,然后序列化您的 Json 数据,并将 JsonSerializerSettings 设置为 Unspecified。
公共 ActionResult GetregisteredUsersJSON()
<代码>{
var usersList = this.GetregisteredUsers()
返回内容(JsonConvert.SerializeObject(usersList, new JsonSerializerSettings{DateTimeZoneHandling = DateTimeZoneHandling.Unspecified }));
}
If you save the data in the database without specifying that it is UTC, then you will want to keep JsonResult from converting the DateTime during serialization. To do this you will want to use ActionResult instead of JsonResult, then serialize your Json data with the JsonSerializerSettings set to Unspecified.
public ActionResult GetregisteredUsersJSON()
{
var usersList = this.GetregisteredUsers()
return Content(JsonConvert.SerializeObject(usersList, new JsonSerializerSettings{DateTimeZoneHandling = DateTimeZoneHandling.Unspecified }));
}
我强烈怀疑这是您的服务器端的问题,并且误解了格式化的客户端值。
您应该发送 UTC 值,但看起来您正在发送本地值。我假设数据库中的值应该是 UTC 值 - 否则整个事情一开始就变得有些随意。当服务器更改时区时,您发送不同的值这一事实应该是一个很大的警告灯。当您调用仅采用毫秒值的 Date 构造函数时,这意味着自 UTC 1970 年 1 月 1 日以来的毫秒数。
假设您的值是 1900 UTC,您应该在 IST 客户端上看到“Fri Jul 23 00:30:00 UTC+0530 2010”,以及“Thu Jul 22 12:00:00 UTC -0700 2010”在您的 EST 客户端上 - 因为这两个时间代表相同的 UTC 时间。
你的服务器代码是什么样的?
I strongly suspect this is a problem on your server side, as well as misinterpreting the formatted client side value.
You should be sending down the UTC value, whereas it looks like you're sending down the local value. I assume the value in your database is meant to be a UTC value - otherwise the whole thing becomes somewhat arbitrary to start with. The fact that you're sending down a different value when your server changes time zone should be a big warning light. When you call the Date constructor taking just a milliseconds value, that's meant to be milliseconds since 1st January 1970 UTC.
Assuming your value is meant to be 1900 UTC, you should see "Fri Jul 23 00:30:00 UTC+0530 2010" on your IST client, and "Thu Jul 22 12:00:00 UTC-0700 2010" on your EST client - because both of those represent the same UTC time.
What does your server code look like?
今天我也发生了同样的事情。我正在使用 sql server 日期字段,只是通过 MVC 将从数据库检索到的日期转换为 json。我无法真正解释造成差异的原因,但我怀疑它与与我的本地环境不同步的 sql server 08 R2 更新有关。
不管怎样,我只需在 C# 代码中设置日期就可以修复它。为了便于使用,我创建了一个扩展方法:
The same thing was happening to me today. I'm using the sql server date field and just converting dates retrieved from the DB to json via MVC. I can't really explain what is causing the difference, but I suspect it's related to sql server 08 R2 updates that are out of sync with my local environment.
Anyway, I was able to fix it by just setting the date only in the c# code. I created an extension method for ease of use: