I ran into the same problem because I like to pass my dates as Unix timestamps, and I had a requirement to display them using different formats depending on the context. Here is how I did it.
If you are loading data through storage, you can use the conversion function provided by Ext.data.Field. For instance:
var fields = [ {name: 'sysTestedDateObj', mapping: 'sysTestedDateTS', type: 'date', dateFormat: 'timestamp'}, {name: 'sysTestedDate', convert: function(v, rec){ return fmtDate('sysTestedDateTS', rec); }}, {name: 'targetChangeStartDate', convert: function(v, rec){ return fmtDate('targetChangeStartDateTS', rec); }}, {name: 'createDateTime', convert: function(v, rec){ return fmtDateTime('createDateTS', rec); }}, {name: 'modifyDateTime', convert: function(v, rec){ return fmtDateTime('modifyDateTS', rec); }}, ]; var store = new Ext.data.JsonStore({ ... fields: fields });
Here are some conversion functions:
function fmtDate(sf, rec) { if ( rec[sf] ) { var dt = new Date(); dt.setTime(rec[sf] * 1000); return dt.format('lj F Y'); } }; function fmtDateShort(sf, rec) { if ( rec[sf] ) { var dt = new Date(); dt.setTime(rec[sf] * 1000); return dt.format('D j M Y'); } }; function fmtDateTime(sf, rec) { if ( rec[sf] ) { var dt = new Date(); dt.setTime(rec[sf] * 1000); return dt.format('lj FY h:i a'); } }; function fmtDateTimeShort(sf, rec) { if ( rec[sf] ) { var dt = new Date(); dt.setTime(rec[sf] * 1000); return dt.format('D j MY h:i a'); } };
Where sf is the source field, we output the formatted date string from.
Please note the following: this is important. The convert () function is presented with a copy of the data record read by the reader (this is in ExtJS docs). This means that you cannot use any associated fields in your conversions. In the array of fields above, I have a field defined as
{name: 'sysTestedDateObj', mapping: 'sysTestedDateTS', type: 'date', dateFormat: 'timestamp'}
So, I create a sysTestedDateObj date object from the sysTestedDateTS field, and I told the reader that I want it to give me a date object obtained from an object containing a Unix timestamp. This is a good object for future reference, but it will not be part of the data record passed to our conversion function.
Also note that the conversion function may refer to fields in records that are not defined for use by the repository. In the above example, I use the sysTestedDateTS field in the conversion function because I know that the server sends it in a JSON response, but since I did not define it in an array of fields, it will not be accessible through the storage of the consuming component.