我试图从数组“日期”中获取最接近今天的日期。
示例:今天是2011-09-10 - > JSON文件的下一个最近日期是“2012-12-20” - > $('div')。append('date1:'+ dates.date1);
示例2:今天是2012-12-21 - > JSON文件的下一个最近日期是“2012-12-25” - > $('div')。append('date2:'+ dates.date2);
尝试了几个解决方案,但没有成功。需要帮忙!这是JSON文件。
[
{
"fields": {
"field1": "Field1",
"field2": "Field2"
},
"dates": {
"date1": "2012-12-20",
"date2": "2012-12-25",
"date3": "2012-12-31"
}
}
]
答案 0 :(得分:1)
假设array
是保存解析后的JSON字符串的变量。这是实现目标的一种方式:
var dates = array[0].dates;
var current = (new Date).getTime();
var closest = null;
var closestDateDif = current;
var i, j;
for (i in dates){
j = current - new Date(dates[i]).getTime();
//j is a temporary variable which now holds the difference between the time stamp and now
if(j >= 0 && j < closestDateDif){
closest = i;
closestDateDif = j;
}
}
//dates[closest] holds the closest date.
//You can even calculate the remainder between now and the next time stamp, for example:
var daysLeft = Math.floor(closestDateDif/24e3/3600);
此版本仅使用Date对象一次。
var dates = array[0].dates;
var now = new Date();
var UNIQUENAME = "current/randomstringwhich doesn't exist in date";
//this variable is used to determine whether you've reached the current date or not
function pad(x){return x>9?x:"0"+x}
now.getFullYear() + "-" + pad(now.getMonth()+1) + "-" + pad(now.getDate());
var tmp = [[UNIQUENAME, now]];
for(i in dates){
tmp.push([i, dates[i]]);
}
tmp.sort(function(x,y){return x[1].localeCompare(y[1])});
for(var i=0; i<tmp.length; i++){
if(tmp[i][0] == UNIQUENAME){
i++;
break;
}
}
if(tmp[i]) alert("The closest date is at " + tmp[i][1] + ". Description: " + tmp[i][0]);
else alert("All dates are in the past!")
//tmp[i][1] holds the value of the closest next date
//tmp[i][0] holds the value of the descriptor.
答案 1 :(得分:0)
无论是谁有这样的解决方案.... 请把它放回去:)
var json = [
{
"fields": {
"field1": "Field1",
"field2": "Field2"
},
"dates": {
"date1": "2012-12-20",
"date2": "2012-12-25",
"date3": "2009-12-31",
"date4": "2010-12-31"
}}
]
var dates = [];
var jsondates = json[0].dates;
for (var i in jsondates) {
dates.push(new Date(jsondates[i]))
}
dates.sort(function(a,b){
return Math.abs(1 - a / new Date()) - Math.abs(1 - b / new Date())
});
// dates is now ordered by closest to today with index 0 being the closest.
所需要的只是一种排序算法
更仔细地阅读这个问题...你不希望任何日期在过去的当前日期被考虑,即使它更接近......这也不难:
for (var i in jsondates) {
if(new Date(jsondates[i]) >= new Date())
dates.push(new Date(jsondates[i]))
}
dates.sort(function(a,b){
return Math.abs(1 - a / new Date()) - Math.abs(1 - b / new Date())
});
答案 2 :(得分:0)
我意识到已经有了一个已经接受的答案,但只要你有ECMA5 Arrays&amp; amp;可用对象(您可以在旧版浏览器上对其进行填充,您应该这样做。)
首先,从JSON字符串中获取日期:
var dates = [], datesObj = JSON.parse(string)[0].dates;
for(var d in datesObj){ dates.push(d); }
通过将日期存储为数组,您可以在这里省去一些麻烦,因为您已经丢弃了密钥。
一旦你将它们作为一个数组,你就可以对它们进行排序,而不需要使用单行代码的先前日期:(为了便于阅读,分为多行)
var upcomingDates = dates.map(function(d){
return new Date(d.replace(/(\d+)-(\d+)-(\d+)/,"$2/$3/$1"));
}).filter(function(d){
return d < new Date();
}).sort(function(a,b){
return a - b;
});
使用本机数组方法可以省去很多麻烦。如果您之前需要显示它们,就可以使用Date.getFullYear
,Date.getDate()
和Date.getMonth()
来格式化它们。