Json returns different dates when local machine and server in different timezones

家住魔仙堡 提交于 2020-06-01 07:18:45

问题


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();
}

回答1:


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?




回答2:


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 })); }




回答3:


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:

    public static DateTime DateOnly(this DateTime date)
    {
        return new DateTime(date.Year, date.Month, date.Day);
    }


来源:https://stackoverflow.com/questions/3332498/json-returns-different-dates-when-local-machine-and-server-in-different-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!