2

我有一个 JSON 对象,它返回带有日期和时间的事件列表,我想为下一个事件时间添加一个计数器,但我不确定如何处理这个?

基本上,我的 JSON 是这样的:

[{"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"}, ....

所以对于每个对象:

{“名称”:“贝克十字路口”,“国家”:“美国”,“经度”:“165.25”,“产量”:“21”,“纬度”:“11.58”,“深度”:“.027” ", "日期": "1946/07/24 21:35:00", "id": "1"}

{“name”:“VENUS”,“country”:“USA”,“lon”:“-116.2”,“yield”:“10”,“lat”:“37.19”,“depth”:“.03” ,“日期”:“1958/02/22 01:00:00”,“id”:“10”,“填充”:“绿色”}

...

这是读取 json 文件的代码:

    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)
        }
    });
}

我想计算日期/时间差,所以在这种情况下,“1958/02/22 01:00:00”和“1946/07/24 21:35:00”的差异并将其传递给消息数组?

4

1 回答 1

2

要以毫秒为单位有这种差异,您可以使用

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

那是因为您拥有的格式可以直接由 Date 类解析。

于 2012-08-24T13:08:38.003 回答