Why does ExtJS subtract a day when formatting a date?

陌路散爱 提交于 2019-12-12 10:53:25

问题


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");

回答1:


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");



回答2:


This problem exists in Ext3, but the solution is slightly different:

var dt = '2012-01-31'; //date string
dt = Date.parseDate(dt, "Y-m-d");
Ext.util.Format.date(dt, 'm/d/Y'); //returns 01/31/2012



回答3:


If you're unable to use Gregor's answer (e.g. filling a grid), note that changing the input to a non ISO 8601 date format will avoid the UTC parsing as well. For example

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

will give 2012-01-13



来源:https://stackoverflow.com/questions/9023641/why-does-extjs-subtract-a-day-when-formatting-a-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!