2

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 corresponding 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!

Oscar Godson
  • 31,662
  • 41
  • 121
  • 201
  • Please provide a JSON snippet so we can be sure it really is valid JSON, and that the behavior you see is not due to the debug tools reviving objects for you automagically. – haylem Nov 26 '10 at 10:52

2 Answers2

2

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.

Community
  • 1
  • 1
haylem
  • 22,460
  • 3
  • 67
  • 96
  • Hmm I'm not sure if I understand or you misunderstand... I know how to do feature detection and I know it's a date because it's being put into the JSON as new Date() but when I cant figure out how to select it via JSON syntax. Everything I have tried returns undefined... Hopefully that makes more sense? – Oscar Godson Nov 25 '10 at 20:50
  • In short, I know it's a date, I know it's supported, AND it's a object using typeof and I can see it with web inspectors I just cant figure out the syntax to select the date... – Oscar Godson Nov 25 '10 at 20:51
  • @Oscar Godson: please see my edit. And please provide a JSON snippet. – haylem Nov 26 '10 at 10:51
  • Thanks a ton! ok, i updated, and great explanation. Ill look into it, but i did include a lot more code. – Oscar Godson Nov 27 '10 at 04:45
0

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):

  1. 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);

  2. from a command prompt, run:

    wscript test.js

Graham Laight
  • 4,700
  • 3
  • 29
  • 28