Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JSON object which returns a list of events with dates and times, I want to add a counter to the next event time, but i am not sure how to approach this?

Basically, my JSON is like:

[{"name": "BAKER-CROSSROADS", "country": "USA", "lon": "165.25", "yield": "21", "lat": "11.58", "depth": ".027", "date": "1946/07/24 21:35:00", "id": "1"}, {"name": "VENUS", "country": "USA", "lon": "-116.2", "yield": "10", "lat": "37.19", "depth": ".03", "date": "1958/02/22 01:00:00", "id": "10", "fill": "green"}, {"name": "HATCHIE", "country": "USA", "lon": "-116.03", "yield": "20", "lat": "37.058", "depth": ".061", "date": "1963/02/08 16:00:01", "id": "100", "fill": "green"}, {"name": "CLIMAX:Upshot-Knothole", "country": "USA", "lon": "-116.0183", "yield": "61", "lat": "37.0875", "depth": "-.41", "date": "1953/06/04 11:14:57", "id": "1000", "fill": "red"}, {"name": "BRAVO:Castle", "country": "USA", "lon": "165.274", "yield": "15000", "lat": "11.698", "depth": "0", "date": "1954/02/28 18:45:00", "id": "1001", "fill": "red"}, ....

so for each object:

{"name": "BAKER-CROSSROADS", "country": "USA", "lon": "165.25", "yield": "21", "lat": "11.58", "depth": ".027", "date": "1946/07/24 21:35:00", "id": "1"}

{"name": "VENUS", "country": "USA", "lon": "-116.2", "yield": "10", "lat": "37.19", "depth": ".03", "date": "1958/02/22 01:00:00", "id": "10", "fill": "green"}

...

here is the code that reads the json file:

    this.loadNext = function () {
    d3.json("./data/detonations.json", function(datum) {
        for(var i = datum.length - 1; i >= 0; --i) {
            var o = datum[i];
            //console.log(o);
            message = {
                country: o.country
                ,date: o.date
                ,depth: o.depth
                ,lon: o.lon
                ,lat: o.lat
                ,type: o.type
                ,yield: o.yield
            };
            self.doSomething(message)
        }
    });
}

i would like to calculate the date/time difference, so in this case the difference of "1958/02/22 01:00:00" and "1946/07/24 21:35:00" and pass this to the message array?

share|improve this question

1 Answer

up vote 0 down vote accepted

To have this difference in millisecondes, you may use

var diff = (new Date(s2)).getTime() - (new Date(s1)).getTime();

That's because the format you have is directly parsable by the Date class.

share|improve this answer
but the this.loadNext reads the json file and loops through it, so i would need to have a variable s1 = none; initially and then update s1 as this.loadNext updates, is this right? – khinester Aug 24 '12 at 13:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.