[英]Getting next closest date from JSON
我正在尝试从数组“日期”中获取与今天最接近的日期。
例如:今天是2011-09-10-> JSON文件中的下一个最接近的日期是“ 2012-12-20”-> $('div')。append('date1:'+ date.date1);
示例2:今天是2012-12-21-> JSON文件中的下一个最接近的日期是“ 2012-12-25”-> $('div')。append('date2:'+ date.date2);
尝试了一些解决方案,但没有成功。 需要帮忙! 这是JSON文件。
[
{
"fields": {
"field1": "Field1",
"field2": "Field2"
},
"dates": {
"date1": "2012-12-20",
"date2": "2012-12-25",
"date3": "2012-12-31"
}
}
]
假设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.
不管是谁有这样的解决方案.... 请放回去 :)
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())
});
我知道已经有了一个可以接受的答案,但是只要您有可用的ECMA5阵列和对象(可以在较旧的浏览器中对它们进行匀化即可),有一种更简单的方法可以做到这一点。
首先,从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()
格式化它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.