PDA

View Full Version : Minor Enhancement to DateField



durlabh
27 Aug 2008, 3:04 AM
This enhancements allows the user to enter a value between -365 and 365 and a relative date is chosen. For example:

0 means today
-1 means yesterday
1 means tomorrow


Ext.override(Ext.form.DateField, {
oldParseDate: Ext.form.DateField.prototype.parseDate,

parseDate: function(value) {
if (value && !isNaN(value)) {
var diff = parseInt(value);
if (diff >= -365 && diff <= 365) {
var date = new Date().clearTime();
date = date.add(Date.DAY, diff);
return date;
}
}
return this.oldParseDate.call(this, value);
}
});

Vlatombe
13 Sep 2008, 12:01 PM
Hello,

I made it in an extension instead of an override. I removed the limit of -365 and 365 which is already covered if you use minValue/maxValue. And finally since isNaN(new Date()) == false, I added this extra check which caused the field to display NaN/NaN/NaN every time.



Ext.ux.RelativeDateField = Ext.extend(Ext.form.DateField, {
allowRelative: true,

parseDate: function(value) {
if (this.allowRelative && value && !Ext.isDate(value) && !isNaN(value)){
var diff = parseInt(value);
var date = new Date().clearTime();
date = date.add(Date.DAY, diff);
return date;
}
return Ext.ux.RelativeDateField.superclass.parseDate.call(this, value);
}
});