PDA

View Full Version : How to decode JSON with date in M$ DateTime format



Ex_Soft
13 Jan 2012, 1:24 AM
How to decode JSON with date in M$ DateTime format?


<!doctype html>
<html>
<head>
<script type="text/javascript" src="http://docs.sencha.com/ext-js/4-0/extjs/ext-all.js"></script>
<script type="text/javascript">
Ext.onReady(function() {
var
tmpStr="{\"FBool\":true,\"FString\":\"string\",\"FInt\":1,\"FFloat\":1.1,\"FDouble\":11.11,\"FDecimal\":111.111,\"FDateTime\":\"\/Date(1326443637436)\/\"}",
o;

try
{
o=Ext.decode(tmpStr);

if(window.console && console.dir)
console.dir(o);
}
catch(err)
{
if(window.console && console.log)
console.log("%s Can't decode \"%s\" : %s", err.name, tmpStr, err.message);
}
});
</script>
</head>
<body>
</body>
</html>



...
FDateTime: "/Date(1326443637436)/"
...

Ex_Soft
13 Jan 2012, 1:46 AM
I use


tmpStr=tmpStr.replace(/\"\/Date\((-?\d+)\)\/\"/g,"new Date($1)");

Does exist any native ExtJS way to resolve this issue, doesn't it?

friend
13 Jan 2012, 5:31 AM
var dateObject = Ext.Date.parse(o.FDateTime, 'MS');
var ymdString = Ext.util.Format.date(dateObject, 'Y-m-d');
console.log('Date String in Y-m-d format is ' + ymdString);

Ex_Soft
13 Jan 2012, 5:50 AM
var dateObject = Ext.Date.parse(o.FDateTime, 'MS');

THNX