I’m having issues displaying the correct date on the client’s browser using a JSON serialized object. The user is able to define what time zone they want to view data as. Given this, I convert the UTC date to the user’s time zone on the server. Then I want to serialize the date/times (which are already converted to their defined time zone) to the browser via JSON.
Seems simple, however the JSON serializers I’ve been using have been severely mucking up my dates. The server is in UTC and the client is in Central (-6). The dates are being ajusted (-12 hours) even though I am specifying the DateTime.Kind to Unspecified.
Somehow .NET knows what time zone the client’s browser is at and what time zone the server is in and it is negating -6 from my dates/times even though I’ve already ajusted the time per the user’s global settings and set the dates’ kind to be unspecified. How can I get the JSON serializers to NOT try and adjust my dates?
List<ErrorGridModel> models = Mapper.Map<ErrorCollection, List<ErrorGridModel>>(errors);
foreach (ErrorGridModel m in models)
{
//convert UTC dates to user local dateTime - split out date vs. time for grouping & splitting columns
DateTime dtLocal = TimeZoneInfo.ConvertTimeFromUtc(m.ErrorDate, this.AppContext.User.TimeZoneInfo);
m.ErrorDate = new DateTime(dtLocal.Year, dtLocal.Month, dtLocal.Day, 0, 0, 0, DateTimeKind.Unspecified);
m.ErrorTime = new DateTime(1900, 1, 1, dtLocal.Hour, dtLocal.Minute, dtLocal.Second, DateTimeKind.Unspecified);
}
IQueryable<ErrorGridModel> dataSource = models.AsQueryable();
return new ContentResult() { Content = JsonConvert.SerializeObject(dataSource.ToDataSourceResult(request), new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }), ContentType = "application/json" };
//return Json(dataSource.ToDataSourceResult(request));
ISO dates appear to work, but I can’t use them as I have 3rd party controls that want the older Microsoft format… which adjusts the time zones on me.
Here is a long discussion on the exact situation that I have been in. http://www.telerik.com/community/forums/aspnet-mvc/grid/grids-and-dates.aspx
Bottom line, if you are using the Microsoft JSON date format, it will always reflect the date in UTC as the number of milliseconds (ticks) from 1/1/1970 UTC. There’s no way for me to auto-convert the time to local on the server and send what it should be down via JSON to the Kendo Grid as the Kendo Grid control instantiates the date from the ticks in JS as UTC. When displaying this date, it will auto-convert the value to the browser’s local time zone.
The only way to show my server’s converted date value from the server is to send the date via JSON as a string to the client.