我有一个JSON对象,它返回一个包含日期和时间的事件列表,我想在下一个事件时间添加一个计数器,但我不知道如何处理它?</ p>
基本上,我的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"}, ....
所以每个对象:
{&#34; name&#34;:&#34; BAKER-CROSSROADS&#34;,&#34; country&#34;:&#34; USA&#34;,&#34; lon&#34; :&#34; 165.25&#34;,&#34; yield&#34;:&#34; 21&#34;,&#34; lat&#34;:&#34; 11.58&#34;,&# 34;深度&#34;:&#34; .027&#34;,&#34;日期&#34;:&#34; 1946/07/24 21:35:00&#34;,&#34; id&# 34;:&#34; 1&#34;}
{&#34; name&#34;:&#34; VENUS&#34;,&#34; country&#34;:&#34; USA&#34;,&#34; lon&#34;:& #34; -116.2&#34;,&#34;收益&#34;:&#34; 10&#34;,&#34; lat&#34;:&#34; 37.19&#34;,&#34 ;&#34;:&#34; .03&#34;,&#34; date&#34;:&#34; 1958/02/22 01:00:00&#34;,&#34; id&#34 ;:&#34; 10&#34;,&#34;填充&#34;:&#34;绿色&#34;}
...
这是读取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)
}
});
}
我想计算日期/时间差异,所以在这种情况下,差异是&#34; 1958/02/22 01:00:00&#34;和&#34; 1946/07/24 21:35:00&#34;并将其传递给消息数组?
答案 0 :(得分:2)
要在毫秒级别中有这种差异,您可以使用
var diff = (new Date(s2)).getTime() - (new Date(s1)).getTime();
那是因为您拥有的格式可以由Date类直接解析。