This is a migrated thread and some comments may be shown as answers.

json stringify and DatePicker value

2 Answers 789 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 18 Jun 2012, 12:31 PM
I am using JSON.stringify to create a value stored in a database on the server side.  When the value comes back and is placed in datepicker the value does not look right.

This fiddle mimics the process.
http://jsfiddle.net/RichardAD/jfugG/

Something wonky is up because the datepicker icons don't appear and the css is barfed.

I also tried using obj.data('kendoDatePicker').value().getTime() for storage and that didn't not work either.

How do YOU serialize and deserialize dates used with DatePickers ?

2 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 21 Jun 2012, 08:44 AM
Hi Richard,

The reason for the problem is that the datestring cannot be automatically parsed to a valid date object. This behaviour can be observed without Kendo:
var dateString = JSON.stringify(new Date());
new Date(dateString); //Invalid date

You can try to:
  • build a JavaScript Date object manually from the returned string, 
  • return date in another format from the server
  • use another approach to deserialize the date on the client

For example:
dateval = $("#date1").data('kendoDatePicker').value();
datestring = dateval.toString();
$("#msg").append(datestring+'<br>');
$("#date2").data('kendoDatePicker').value(datestring);

For convenience I have updated your example and included the datePicker widget formatting options, please check the result.

Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Richard
Top achievements
Rank 1
answered on 21 Jun 2012, 10:39 AM
Thank you for the example Alexander.

My interim solution was to explicitly create a string that Date implicitly parses

function MDY (date) {
return (date.getMonth()+1) + '/' + date.getDate() + '/' + date.getFullYear() ;
}

... event 
config.sales.date0:    MDY($('#sales-date-begin').data('kendoDatePicker').value()),
... store stringified object in server db
... event
... query stringified object from server db 
$("#sales-date-begin").data('kendoDatePicker').value(config.sales.date0);

Regards,
RichardAD
Tags
Date/Time Pickers
Asked by
Richard
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Richard
Top achievements
Rank 1
Share this question
or