View Full Version : ASP.net JSON datetime exchange problem

1 Jul 2010, 6:34 AM
I have problem at reading the json date from extjs please help.

My WCF web service return this json object to extjs:


but extjs grid couldn't display the record. I believe it is because the date format again.

I use this to override the json encoder:

Ext.util.JSON.encodeDate = function (d) {
return d.format('M$');

Any idea what is going wrong?

another problem is this, my web service return error when extjs send JSON object with date.


this is the error WCF web service return:
Encountered unexpected character '\'.'

could you see why?

this is my extjs store:
var MonthlyReportColomn = [
//{header:'??', dataIndex:'Date', sortable:true, width:50, renderer:renderDate}
{header: 'Year Month', width: 300, sortable: true, dataIndex: 'MonthYear', editor: new Ext.form.DateField({format:'Y'}), allowBlank: true },
{ header: 'Remark', width: 500, sortable: true, dataIndex: 'Remark', editor: new Ext.form.TextField(), allowBlank: true },
{ id: 'ID', header: 'ID', width: 100, sortable: true, dataIndex: 'ID' },
{ header: 'Company ID', width: 100, sortable: true, dataIndex: 'CompanyID' }
var MonthlyReportProxy = new Ext.data.HttpProxy({
// Call web service method using GET syntax
read: {
method: 'GET',
url: '/WebServices/MonthlyReportService/MonthlyReportService.svc/GetAllbyCompanyId'
create: '/WebServices/MonthlyReportService/MonthlyReportService.svc/Add',
update: '/WebServices/MonthlyReportService/MonthlyReportService.svc/Update',
destroy: '/WebServices/MonthlyReportService/MonthlyReportService.svc/Delete'
var MonthlyReportReader = new Ext.data.JsonReader({
totalProperty: 'TotalCount',
successProperty: 'success',
idProperty: 'ID',
root: 'Data',
messageProperty: 'message',
// map to MonthlyReportDTO
//fields: [{ name: 'MonthYear', type: 'date', dateFormat: 'M$' }, 'Remark', 'ID', 'CompanyID']
fields: [ {name: 'MonthYear', type: 'date', dateFormat: 'Y'}, 'Remark', 'ID', 'CompanyID']
var MonthlyReportWriter = new Ext.data.JsonWriter({
encode: false,
writeAllFields: true
var MonthlyReportStore = new Ext.data.Store({
autoLoad: true,
proxy: MonthlyReportProxy,
reader: MonthlyReportReader,
writer: MonthlyReportWriter,
autoSave: true,
batch: false


1 Jul 2010, 7:14 AM
You set Ext.util.JSON.encodeDate


So you told Ext how you want it to encode dates to send in JSON.

And as for what your webservice sends...


That is not valid JSON.

JSON is Javascript, so type

foo = {"MonthYear":\/Date(1277913600000)\/}

Into the Firebug console

1 Jul 2010, 8:02 AM
Sorry I am confused, maybe I am too green at javascript.

Maybe I should clarify my problems again too.

1) My .net service send the below JSON object back to extjs, but extjs could not display it, how should I config my extjs code to enable my extjs grid to display this in my grid?


2) Another similar question. When I added a record in extjs grid, extjs sent the below JSON object back to my .net web service, but my .net web service return error , complaint the format. I could only tell the difference is \/Date(1277913600000)\/ without double quote. Could you see anything wrong with this?

JSON object from extjs to .net service:


error message return from .net service:

Encountered unexpected character '\'.'

1 Jul 2010, 9:24 AM
So, the MonthYear field needs dateFormat: 'M$' for ExtJS to READ that into a Date object.

Let's fix that first.

1 Jul 2010, 5:28 PM
first problem solved. I changed the reader dateformat, and it works.

I also has a clue about the second issue. I created a thread (http://social.msdn.microsoft.com/Forums/en/wcf/thread/ec687986-bdcf-4cae-8d1a-3231196ab142) at the WCF forum about it. people reply the date \/Date(...)\/ need to be between quote. So how do I config extjs to add quote to the date?

1 Jul 2010, 9:11 PM
You've overridden Ext.util.JSON.encodeDate to break the implementation! Remove that!

1 Jul 2010, 9:49 PM
If I don't override it, the date format become like this:

{"d":{"Remark":"","MonthYear":"Fri Jul 02 2010 13:40:51 GMT+0800 (China Standard Time)","CompanyID":137}}

So where and how should I config my grid? or store? to send this\/Date(1277913600000)\/ format?

1 Jul 2010, 9:55 PM
OK, then override it to add quotes!

1 Jul 2010, 11:17 PM
ok this work

Ext.util.JSON.encodeDate = function (d) {
return "\""+ d.format('M$') + "\"";

But when I create new record, I was trying to pre-select date to now so i use MonthYear: Date(), but it doesn't send the \/date()\/ format. Why is this happening?

id: 'MonthlyReportGridAddbtn',
text: 'Add',
handler: function () {
var companyID = grid.getSelectionModel().getSelected().get('ID');
var newMonthlyReport = new MonthlyReportGrid.store.recordType({
Remark: '',
// MonthYear: Date(),
MonthYear: '',
ID: '',
CompanyID: companyID
MonthlyReportGrid.store.insert(0, newMonthlyReport);

1 Jul 2010, 11:33 PM
new Date()