最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Kendo DateTimePicker Not Handling UTC Offset - Stack Overflow

programmeradmin1浏览0评论

We are using the Kendo datetimepicker, implemented using the AngularJS directives:

<input type="text" kendo-date-time-picker k-ng-model="TheDateModel">

Where: TheDateModel = 2016-02-15 20:58:24.0000000 +00:00

I am in the CST timezone, which is -6 hour offset from the GMT. The current result of the datetimepicker shows a time of 8:58 pm but my expected result is 2:58 pm.

What in the world am I doing wrong?

We are using the Kendo datetimepicker, implemented using the AngularJS directives:

<input type="text" kendo-date-time-picker k-ng-model="TheDateModel">

Where: TheDateModel = 2016-02-15 20:58:24.0000000 +00:00

I am in the CST timezone, which is -6 hour offset from the GMT. The current result of the datetimepicker shows a time of 8:58 pm but my expected result is 2:58 pm.

What in the world am I doing wrong?

Share Improve this question edited Feb 15, 2016 at 23:58 Mike asked Feb 15, 2016 at 23:50 MikeMike 2352 gold badges8 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Disclaimer: I work for Kendo UI team

The Kendo UI Datepicker uses JavaScript Date object internally to hold the selected date value. As you probably know, it always uses the local (browser) timezone. We tried to explain that caveat in our docs too:

  • JavaScript Date Object - Basics

Due to this default behavior, the widget will use the already converted Date value (with the applied local timezone). The widget doesn't manipulate the value timezone, as it does not have sufficient information how to do that.

Solution

The best approach in this case is to convert the Date strings (like the one you mentioned "2016-02-15 20:58:24.0000000 +00:00") manually before feed the DatePicker widget. For instance, here is one possible approach to do that:

  • http://dojo.telerik./EyuRA

Notice how the value is parsed and then adjusted in the loadData method. Similar thing should be done by the developer that needs to handle different TZ in their app.

I've been down this road and had to implement this:

http://www.telerik./support/code-library/using-utc-time-on-both-client-and-server-sides

So I found the solution to my problem. First off for clarity, and sorry for the misinformation, but my date was ing down from the server as 2016-02-15T20:58:24.0000000+00:00 - add the T and remove all spaces.

All that needed to be done was to add the k-parse-formats attribute to the directive as follows:

 <input type="text" kendo-date-time-picker k-parse-formats=['yyyy-MM-ddTHH:mm:sszzz'] k-ng-model="TheDateModel">

Boom, considers the offset and your current timezone, and correctly parses and displays the date and time. Just be aware, that when you specify your own parse formats, to include all possible formats that your dates could be.

For example, I then ran into a problem where I had milliseconds greater than 0 ing through on my dates: 2016-02-15T20:58:24.1234567+00:00. This broke the datetimepicker again. Simpler fix: just changed my parsing format to: yyyy-MM-ddTHH:mm:ss.fffffffzzz. Make sure the number of f is greater than or equal to the number of possible milliseconds.

<input type="text" kendo-date-time-picker k-parse-formats=['yyyy-MM-ddTHH:mm:ss.fffffffzzz'] k-ng-model="TheDateModel">
发布评论

评论列表(0)

  1. 暂无评论