views:

69

answers:

1

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

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?

Jon Skeet
@Jon, i have updated my question with server code. I am not converting anything in server side for the date, i am just getting the date value from DB and passing to the client
Prasad