1

I am converting a Data Object to JSON and back with JSON.stringify and JSON.parse.

This works great, on all devices but on Samsung Galaxy SII where for the line:

console.log(jsonObj.gebDat+"::"+new Date(jsonObj.gebDat));

I get the output:

1973-07-01T10:49:25.134Z::Invalid Date

I am implementing this exactly like at this answer, and it works for most devices, am I doing something wrong??

UPDATE

to clarify the question. I create a String calling

var stringToSave = JSON.stringify({gebDat: dataclass.gebDat, <here are some more variables>});

then I save it. Later, I load the string and parse it with

var jsonObj = JSON.parse(stringToSave);

then, I try to set my date again (calling a log just before that line) with

console.log(jsonObj.gebDat+"::"+new Date(jsonObj.gebDat));
this.gebDat = new Date(jsonObj.gebDat);

The log gives me the invalid date as shown above, and when I represent the Date it displays NaN.NaN.NaN instead of the expected 01.07.1973

Community
  • 1
  • 1
Daniel
  • 20,420
  • 10
  • 92
  • 149
  • 1
    Is your Galaxy SII on the same Android version than your other devices? – Maen Jul 01 '13 at 10:45
  • @Bigood No, the Galaxy SII is on Android version 2.3.3. – Daniel Jul 01 '13 at 10:47
  • Do you handle dates in different timezones? Other wise try the constructor format `new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])` – sabithpocker Jul 01 '13 at 10:49
  • What is `jsonObj.gebDat`? A string? A Date object? – Xotic750 Jul 01 '13 at 11:10
  • @Xotic750 `jsonObj.gebDat` is a string containing "1973-07-01T10:49:25.134Z" in this case. The string is generated using `JSON.stringify` over a variable of type `Date` – Daniel Jul 01 '13 at 11:38

2 Answers2

1

1.Date string formats are implementation dependent. It is always recommended to use timestamps when you save dates.

var timestamp = Date.parse( new Date() );//1372675910000

Now you can use the saved timestamps to recreate the date later

var date = new Date(1372675910000);//Mon Jul 01 2013 16:21:50 GMT+0530 (India Standard Time)

2.For a simple transition from your current solution, in case you dont handle different timezones,

var dateString = jsonObj.getDat.substring(0,23);
var datePart = dateString.split('T')[0].split('-');
var timePart = dateString.split('T')[1].split(/[:.]/);
var DateOj = new Date(datePart[0], datePart[1], datePart[2], timePart[0], timePart[1], timePart[2]);

Let me clarify 1, with reference to your update.

var stringToSave = JSON.stringify({gebDat: Date.parse(dataclass.gebDat), <here are some more variables>});

var jsonObj = JSON.parse(stringToSave);

console.log('timestamp :' + jsonObj.gebDat);//1372680083000
console.log(new Date(jsonObj.gebDat));//Mon Jul 01 2013 17:31:23 GMT+0530 (India Standard Time)
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • [Date.parse()](http://www.w3schools.com/jsref/jsref_parse.asp) uses a string as parameter. I think using it with a Date parameter would not be a great idea. – Daniel Jul 01 '13 at 11:48
  • The suggession was to store `jsonObj.getDat` as timestamp. Then use that timestamp in date constructor. – sabithpocker Jul 01 '13 at 11:50
  • With `JSON.stringify` the method [toJSON()](http://www.w3schools.com/jsref/jsref_tojson.asp) is automatically called. It should work as good as a timestamp since it uses ISO-8601 standards. – Daniel Jul 01 '13 at 12:00
  • It works with the timestamp. I guess that's because of a bug in the contructor of Date in Android 2.3.3 that was corrected later... – Daniel Jul 01 '13 at 12:28
  • Yes, thay might have missed the rfc and iso recommendations. – sabithpocker Jul 01 '13 at 13:14
0

you need send string and you send callback.

try:

console.log(jsonObj.gebDat+"::"+new Date(jsonObj.gebDat()));