PDA

View Full Version : the question about DateField



szxiaoyin
31 Dec 2010, 2:09 AM
when I input 2010-09 in the Datefield,and move to other field,then the Datefield change to 2010-10 ,help!

My code :
{ xtype: "datefield", id: "dtpYearMonth", width: 90, format: 'Y-m' }

http://www.sencha.com/forum/attachment.php?attachmentid=24032&d=1293790133

skirtle
31 Dec 2010, 12:25 PM
I can explain what is going wrong but whether or not this is technically a bug isn't exactly clear. It also isn't clear to me whether DateField is intended to be used for picking a year and month without a day.

As part of the housekeeping that DateField does when you change the value it makes the following call to parse the date:


... = Date.parseDate('2010-09', 'Y-m');According to the docs (and the code) this method will use today's date for any unspecified elements of the date. Today's date is 31 Dec 2010 and as you haven't specified a day in your date format it will use today's day, i.e. 31. When it then builds the Date object to return it uses 2010 09 31. There is no such date, so it rolls over to 2010 10 01. It is this value that then gets reformatted into Y-m and put back into your DateField.

One workaround is to put this line somewhere in your code:


Date.defaults.d = 14;As stated in the docs, this will set the default value used in parseDate() such that it is 14 rather than today's day. There's nothing special about my choice of 14, it just seems like a mid-month value is the safest choice. Though I don't know of any specific problems with this workaround it does involve changing a global setting so it could potentially break date parsing elsewhere in your application.

szxiaoyin
3 Jan 2011, 7:54 PM
it's too bad,
why,,,

it uses 2010 09 31
not uses 2010 09 01???