如果某个日期早于“今天”,我该如何删除它。如何附加所有项目而不仅仅是日期?我希望我至少走在正确的轨道上。
var future = [
{
"month":"march",
"day":1,
"year":2014,
"movie":"thor"
},
{
"month":"may",
"day":29,
"year":2020,
"movie":"superman"
},
{
"month":"may",
"day":29,
"year":2020,
"movie":"batman"
}
];
var afterToday = [];
future.forEach(function(item){
var today = new Date();
var zzz = new Date(item.day + " " + item.month + " " + item.year);
if(zzz > today)
{
afterToday[length] = zzz;
$('body').append(afterToday);
}
最佳答案
对于现代浏览器尝试
var future = [{
"month": "march",
"day": 1,
"year": 2014
}, {
"month": "may",
"day": 29,
"year": 2020
}, {
"month": "may",
"day": 16,
"year": 2014
}];
var array = ['jan', 'feb', 'march', 'april', 'may'];
var today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
today = today.setMilliseconds(0);
future = future.filter(function (value) {
return new Date(value.year, array.indexOf(value.month), value.day) >= today;
})
console.log(future)
演示:Fiddle
使用 jQuery 跨浏览器
future = $.grep(future, function(value){
return new Date(value.year, $.inArray(array, value.month), value.day) > today;
})
演示:Fiddle
关于javascript - 比较日期,如果早于今天则删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23691599/