Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo MVC Datepicker timezone translation

We are observing the following issue related to the time differences between our MVC app and a Kendo DatePicker. The web server is running in UTC+0. The web clients are running in different time zones (UTC+1, UTC+3, UTC-5, etc.)

The web page contains a Kendo date picker (no time portion) where user selects a single day or a month. Behind the scenes The date is sent in an AJAX request using full Date objects which contain the time and timezone information.

We are only interested in the date portion irrespective of the time zone that client is in. When a user selects a date/month we want to receive a C# DateTime object in the local server time.

For example:

User is in UTC+1 and selects ‘01/07/2013’ The actual C# DateTime object is instantiated as ‘30/06/2013 23:00’ We expect to get ‘01/07/2013 00:00:00’

We are currently using the following setting in Global.asax :

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandliig = Newtonsoft.Json.DateTimeZoneHandling.Local;

Could you please advise?

like image 556
maulemon Avatar asked Jul 17 '13 08:07

maulemon


People also ask

Is there a free version of the Kendo UI datepicker?

To try it out sign up for a free 30-day trial. The Telerik UI DatePicker HtmlHelper for ASP.NET MVC is a server-side wrapper for the Kendo UI DatePicker widget. The DatePicker enables the user to select a date from a calendar or through a direct input.

Why Kendo UI date/time pickers using Date object expose the same behavior?

In general, JavaScript Date object is the one responsible for timezone adjustment, which means that every Date instance is created in local timezone and that is why the time is adjusted. Kendo UI Date/Time pickers using Date object internally and that is why expose the same "timezone adjustment" behavior.

How can kendo datetime pickers be used for emergency drills?

I am building a web application that collects information about emergency drills, such as evacuation drills, tornado drills, etc. As you could imagine recording what time the drill was conducted is extremely important. Well, the Kendo DateTime pickers are adding the timezone offset to the time value when the value is sent to the server.

Does kendo datetime work in Eastern Time Zone?

Well, the Kendo DateTime pickers are adding the timezone offset to the time value when the value is sent to the server. Receiving from the server works fine. Everybody that is using my web application is in eastern time zone, no exceptions.


1 Answers

The problem is the conversion gets all borked up between JavaScript Date and C# DateTime. What I do is I convert the date into a string prior to it being posted to the server. You can hook into the Kendo DataSource's parameterMap function to do this.

Using the parameterMap function:

var ds = new kendo.data.DataSource({
  transport: {
    parameterMap: function(data, type) {
      if (type === 'create' || type === 'update') {
        // this changes the date to 'dd/MM/YYYY' format
        data.date = kendo.toString(data.date, 'd');
      }
      return data;
    }
  }
});

I also set the JsonFormatter.SerializerSettings.DateFormatString = "YYYY/dd/MM hh:mm:ss" because JavaScript can convert this to a Date object without error or other wonkiness. I would also recommend changing your DateTimeZoneHandling to UTC. You don't need the timezone offset, do you?

On the server-side in your controller, you can use DateTime.Parse() or let the compiler do the conversion implicitly.

like image 138
Brett Avatar answered Sep 22 '22 03:09

Brett