UTC Date and kendo.toString(d, "u") is incorrect

1 Answer 52 Views
General Discussions
Ryan
Top achievements
Rank 1
Ryan asked on 04 Dec 2024, 04:51 PM

I was testing the UTC "u" date format and noticed it does not adjust the date/time correctly. The "u" only adds the "Z" indicator and does not shift the time. I opened the date formatting example in kendo Dojo and noticed the issue.

const d = kendo.parseDate("2024-12-04T16:30:45Z");
console.log("Source = " + d); // Wed Dec 04 2024 10:30:45 GMT-0600 (Central Standard Time)
console.log("Format 'u' = " + kendo.toString(d, "u")); // 2024-12-04 10:30:45Z
console.log("toISOString = " + d.toISOString());       // 2024-12-04T16:30:45.000Z

Notice that "u" prints the time as 10:30 instead of 16:30 as in "toISOString".

Reference documentation: https://docs.telerik.com/kendo-ui/globalization/intl/dateformatting

Here is my modified version of the date formatting example. Compare the output of "u" to "toISOString": https://dojo.telerik.com/gPLZTAIt/2

 

 

1 Answer, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 09 Dec 2024, 08:54 AM

Hi Ryan,

As described in the Default Date Formats section the 'u' format renders universal sortable UTC date/time pattern ("yyyy-MM-dd HH:mm:ssZ" for en-US). It is expected the 'u' format to display the same time as the rest of the formats for example 'g' or 'F', as it is also in the Dojo example in your email.

Regards,
Neli
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Ryan
Top achievements
Rank 1
commented on 09 Dec 2024, 04:26 PM

No, this is incorrect. The "Z" means "Zulu" time that is equivalent to UTC. The "u" format is printing client/browser local time and appending a "Z" at the end, instead of converting to UTC time and then printing the date/time. The description in the standard Date format specifically states "UTC" whereas 'g' or 'F' uses "local" time.

In my example, I manually specified "2024-12-04T16:30:45Z" so the expected output of "u" should be "2024-12-04 16:30:45Z". Instead it is printing client/browser local time with a "Z" at the end, for example "2024-12-04 10:30:45Z" on my system since I am in Central Standard Time -06:00).

I have date strings in UTC from a web service that are displaying incorrectly because the "u" format is printing local time instead of UTC. I could manipulate the date string and remove the "Z" before it reaches the kendoGrid, so my browser would assume the date is local, but that is a bad hack in my opinion.

Here is the expected result: https://dojo.telerik.com/QbihrrJa/2

      // HACK: This is the expected result for Date format "u"
      // Remove the 'T' and milliseconds from the Date
      function fix_u(d) {
        return d.toISOString().replace(/T/, ' ').replace(/\.[0-9]*/, '');
      }

Neli
Telerik team
commented on 12 Dec 2024, 12:17 PM

Hi Ryan,

The kendo.format and kendo.toString will only format the date and time according to the local time zone of the user. If you need to apply a specific timezone to the date (including UTC), you can use the kendo.timezone.apply method:

- https://docs.telerik.com/kendo-ui/api/javascript/timezone/methods/apply

In order to use the string values instead of numbers(0, 60, 120) you must include the timezone script on your page:

 <script src="https://kendo.cdn.telerik.com/2024.4.1112/js/kendo.timezones.min.js"></script>

Here is a Dojo where this is demonstrated: https://dojo.telerik.com/hZSiSNBI 

Regards,

Neli

 

 

 

Tags
General Discussions
Asked by
Ryan
Top achievements
Rank 1
Answers by
Neli
Telerik team
Share this question
or