Search code examples
dateextjs4

Why does ExtJS subtract a day when formatting a date?


Using ExtJS 4.0.2, I can type the following into the console:

Ext.util.Format.date('2012-01-13', "m-d-Y");

I get 01-12-2012

Why?
I can correct it with:

Ext.util.Format.date('2012-01-13 00:00:00', "m-d-Y");

Solution

  • Ext.util.Format.date in Ext 4.0.2 uses a Date object or a String (your case). This string is parsed using the native Date.parse() using the UTC time zone.

    Try to explicitly parse it using Ext.Date.parse:

    var dt = Ext.Date.parse("2012-01-13", "Y-m-d");
    Ext.util.Format.date(dt, "m-d-Y");