Getting Date and/or checking for it in JSON Object?
I have a JSON object that can (is) storing a Date (and/or any other JS object) but I'm not sure how to get it out once it's in it. For example I have this sample code, but it's not working:
for(var key in value){
if(value.hasOwnProperty(key)){
if(typeof value[key] == 'object'){
if(value[key].Date){
console.log('this is a date')
}
else{
console.log('not a date');
}
}
}
}
However, it just keeps return not a date
. If i inspect the JSON object with Firebug or the Developer Console in WebKit i see __proto__: Date
inside of the corresp开发者_如何学Pythononding JSON item... so, how do I get it out or check for it?
--EDIT--
Here is what i see in the debugger:
Object
->isADate: Fri Nov 26 2010 20:30:57 GMT-0800 (Pacific Standard Time)
--->__proto__: Date
->notADate: "some string"
--->__proto__: Object
And here is the JSON im creating:
var dateStorage = new storageLocker('date-test');
dateStorage.save({'isADate':new Date(),'notADate':'some string'});
Here is the code for this part of my script (http://github.com/oscargodson/storagelocker)
storageLocker.prototype.save = function(value){
var json = JSON.parse(localStorage.getItem(this.catalog));
if(json == null){json = {};}
for(var key in value){
if(value.hasOwnProperty(key)){
json[key] = value[key];
console.log(value[key].Date);
if(typeof value[key] == 'object'){
if(value[key].Date){
console.log('this is a date')
}
else{
console.log('not a date');
}
}
}
}
localStorage.setItem(this.catalog,JSON.stringify(json));
return this;
}
Thanks a lot! Hope this helps out more!
You want to look at this: How to know if an object is a date or not with JavaScript
Basically the "safest" - not 100% failsafe but you should be fine - is to do feature detection.
However, make sure that your JSON entry is really a date object and not a string, in which case this won't work and you'll need to use the new Date()
or Date.parse()
to make it a date object.
EDIT: Following your 2 comments:
JSON does not allow you to store objects in a Date format. So, there's already a discrepancy here. Either you're not really dealing with JSON objects, or you're not dealing with them the way they should be.
See this page for the official documentation on using JSON in JavaScript for information on how to use the reviver parameter of JSON.parse, because a date should be stored as a string and then "revived" to a Date object.
Also, that typeof returns object doesn't necessarily mean that you don't deal with a string, for instance:
typeof new String("test") // object
Now assuming you really do have a Date object at this point, I don't see how testing for value[key].Date
would work anyway. Try it out in your debug console, as you have FireBug:
var t = new Date();
alert(t.Date) // undefined
alert(t.getMonth) // code of the function, so not undefined
alert(typeof t.getMonth != 'undefined') // true
So using a combination of tests for getMonth
, getDay
and others would do the trick.
If, like me, you got here looking for a way to discover what date/time a JSon date value equals, you can do this as follows (using wscript - the Windows script host):
save the following text to a file called test.js (replace 1396566000000 with your own date value)
var date = new Date(1396566000000); WScript.Echo(date);
from a command prompt, run:
wscript test.js
精彩评论