Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
40
DatePickerWrapper EnableUTCDates
posted

My server-side API exposes all dates and times in UTC and I want to display all dates and times to the user in UTC (not the browser's local time).
I am attempting to use the ASP.NET MVC DatePickerWrapper to construct a DatePicker which will display dates and times in UTC.

I have used the following code in my ASP.NET Razor view:
Html.Infragistics().DatePickerFor(d => d.MyDateTimeProperty).EnableUTCDates(true).ID("MyDateTimeProperty").Render()

When bound to a model in C# such that MyDateTimeProperty is 2015-04-10T00:00:00.000Z
MyDateTimeProperty = DateTime.SpecifyKind(new DateTime(2015, 04, 10), DateTimeKind.Utc)

The view fragment is rendered as
$('#MyDateTimeProperty').igEditor({ inputName: 'MyDateTimeProperty', value: new Date(2015, 3, 10, 0, 0, 0, 0), enableUTCDates: true });

This is incorrect because the date will be interpreted by the browser as a local time, not a UTC time. MyDateTimeProperty represents an unambiguous instant in time
because its Kind is Utc, so it should be rendered to HTML and Javascript in a way that preserves this information.

When a browser executes this script it constructs a new Javascript Date object in browser local time. My browser is in the en-GB locale and
this date falls in British Summer Time (our daylight saving time) so I get 2015-04-10:00:00.000+01:00 i.e. 2015-04-09T23:00:00.000Z.
The DatePicker then shows 2015-04-09 when I would expect 2015-04-10.

I use a collection of the same model objects to populate an Ignite UI grid. The data are retrieved using AJAX and the dates are correctly displayed in UTC.
This is because the default JSON serialization in both ASP.NET MVC ("\/Date(1428624000000)\/") and ASP.NET Web API ("2015-04-10T00:00:00.000Z") preserves the instant in time.

Either of the following renderings would preserve the instant in time:

$('#MyDateTimeProperty').igEditor({ inputName: 'MyDateTimeProperty', value: new Date("2015-04-10T00:00:00.000Z"), enableUTCDates: true });
$('#MyDateTimeProperty').igEditor({ inputName: 'MyDateTimeProperty', value: new Date(1428624000000), enableUTCDates: true });

Please would you suggest a work-around for this issue?

Kind Regards,

Merlin

Parents Reply Children
No Data