PDA

View Full Version : Ext.Date.format() only prints locale strings?



kaendsle
28 Mar 2014, 12:56 PM
I have Date objects that were set using a UTC time and I want to recover that time when formatting them using Ext.Date.format(). A very simple example demonstrates that Ext.Date.format() will print a locale string.



var d = new Date('1998-04-15T20:00:00.0000Z');

d.toUTCString();
// "Wed, 15 Apr 1998 20:00:00 GMT"

Ext.Date.format(d, 'H:i')
// "16:00"

Ext.Date.format(Ext.Date.parse('1998-04-15T20:00:00.0000Z', 'c'), 'H:i')
// "16:00"


I need my time string formatted a certain way, and Ext.Date.format() is extremely helpful in that regard. However, I need it to format the date as a UTC string, not a locale string. Relative to the above example, I need a way to output the string "20:00" instead of the string "16:00".

Is this not supported? I will just have to concatenate the outputs from d.getUTCHours() and so forth?

Gary Schlosberg
7 Apr 2014, 8:49 PM
I guess so. Looks like the Date (http://docs.sencha.com/extjs/4.2.1/#!/api/Date) docs page describes something similar in the ISO 8601 formatted dates section:
http://docs.sencha.com/extjs/4.2.1/#!/api/Date

kaendsle
8 Apr 2014, 3:34 AM
This is the best solution I've been able to come up with to mitigate this fact:



Ext.Date.format(Ext.Date.add(date, Ext.Date.MINUTE, date.getTimezoneOffset()), formatString)


...Where date is a Date object in UTC and formatString is the desired PHP-esque format string (http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.Date) to output. The above expression subtracts the timezone offset, producing a Date object with the wrong time, but effectively writing the correct time as a String despite the locale time formatting.