I can't figure out how convert the datetime that wcf outputs into a datetime that ExtJs can use. I have found lots of articles about this but they are all for ExtJs 3 and I couldn't get it to work with 4.
I did find this code but I don't know how I could use it to convert everything in my JsonStore.
//this method is used to convert the MS JSON date format to the ExtJS Grid Date Column开发者_运维百科 Value
function dateFormatter(dt) {
/// <summary>this method is used to convert the MS JSON date format to the ExtJS Grid Date Column Value</summary>
/// <param name="dt">Actual JSON Date Value</param>
try {
//microsoft JSON date format needs to convert into Javascript date
var newdata = dt.replace(/\/Date\((-?[0-9]+)([+-][0-9]+)?\)\//g, "new Date($1)");
newdata = eval('(' + newdata + ')');
return newdata.format('m/d/Y');
}
catch (e) {
return dt;
}
}
Unlike Ext JS 3, Ext JS 4 does not extend the native Date object. Instead it provides Ext.Date. So instead of:
date.format('m/d/Y');
you would instead use:
Ext.Date.format(date, 'm/d/Y');
Additionally using eval() is a really bad idea most of the time. This code is no exception.
And if you drop eval, the try-catch is also not needed.
Finally, a function that both parses a date and converts it to another format seems to be doing too much. Often you will want to display the same date in different formats in different parts of your app. Therefore I would rather just have a function that parses the WCF date format into JavaScript Data object. And then use convert the Date object into particular string format at the very place where it's needed.
Removing all extraneous stuff, this is what I get:
function parseWcfDate(dt) {
var milliseconds = dt.replace(/\/Date\((-?[0-9]+)([+-][0-9]+)?\)\//, "$1");
return new Date(parseInt(milliseconds, 10));
}
Anyway, all this is too much trouble... Ext JS has built-in support for parsing WCF formatted dates:
Ext.Date.parse("/Date(1234567894560)/", "MS");
Also see:
- How to handle json DateTime returned from WCF Data Services (OData)
- How do I format a Microsoft JSON date?
Use JSON.NET with JavaScriptDateConverter.
精彩评论