JSON and Date

sringify and parse

by Denise Nepraunig

HTML

Vom Datum zum JSON String und wieder retour.
String bleibt leider string, deshalb mit new Date...

JavaScript

var date = new Date();
console.log(date.getFullYear());
console.log(date);
//alert(date.toJSON());
var myObj = {
    date : date
};
var jsonData = JSON.stringify(myObj);
console.log(jsonData);

myNewObj = JSON.parse(jsonData);
console.log(myNewObj);
//alert(myNewObj.date);

// doesn't work, because it is a string
//Date.getFullYear(myNewObj.date);

// ohne new bleibt es ein String
myNewObj.date = new Date(myNewObj.date);
console.log(myNewObj.date.getFullYear());
console.log(myNewObj.date);