eval()
is not necessary. This will work fine:
var date = new Date(parseInt(jsonDate.substr(6)));
The substr()
function takes out the /Date(
part, and the parseInt()
function gets the integer and ignores the )/
at the end. The resulting number is passed into the Date
constructor.
I have intentionally left out the radix (the 2nd argument to parseInt
); see my comment below.
Also, I completely agree with Rory's comment: ISO-8601 dates are preferred over this old format - so this format generally shouldn't be used for new development.
For ISO-8601 formatted JSON dates, just pass the string into the Date
constructor:
var date = new Date(jsonDate); //no ugly parsing needed; full timezone support
No.
The JSON is data only, and if you include a comment, then it will be data too.
You could have a designated data element called "_comment"
(or something) that should be ignored by apps that use the JSON data.
You would probably be better having the comment in the processes that generates/receives the JSON, as they are supposed to know what the JSON data will be in advance, or at least the structure of it.
But if you decided to:
{
"_comment": "comment text goes here...",
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
Best Answer
From Gary Court: