Search code examples
javascriptdatedatetimeextjsextjs4

EXTJS Ext.util.Format.date Automatic date conversion


I have a date field which contains data coming in from the database as 2015/07/31 13:01:53.180z. Datetime is stored in UTC on database.

My code looks like this:

  var startDateTime = Ext.util.Format.date(StartDateTime, 'm/d/y g:i:s A');

But the output I get is the conversion of UTC to IST(Indian).I checked on Chrome,Mozilla and IE. I got same output all the time

Does ExtJs does this? Because I haven't wrriten any method for conversion.

I use ExtJs 4.1.1

I would appreciate any help on this.


Solution

  • Timezone is appended in the string->JS Date conversion.

    To parse the date from database without timezone conversion you should use the Ext.Date.parse explicitly, not automatically through model field type 'date' or simply JS constructor new Date().

    For example:

    var db_date = '2015/07/31 13:01:53.180z',
        js_date = Ext.Date.parse(db_date.substring(0,db_date.length-5), 'Y/m/d H:i:s'),
        date_to_show = Ext.util.Format.date(js_date, 'm/d/y g:i:s A');
    

    Obviously "substring" must be replaced by something better, for example you could format db date (cutting timezone part) in the web service serialization.

    If you achieve to clean the date string in the web service you can also add "dateFormat" attribute to model fields to parse date correctly into models.