Javascript 比较日期,如果为';It’他比今天老

Javascript 比较日期,如果为';It’他比今天老,javascript,jquery,arrays,Javascript,Jquery,Arrays,如果日期早于“今天”,我将如何删除它。我如何附加所有项目而不仅仅是日期?我希望我至少在正确的轨道上 var future = [ { "month":"march", "day":1, "year":2014, "movie":"thor" }, { "month":"may", "day":29, "year":2020, "movie":

如果日期早于“今天”,我将如何删除它。我如何附加所有项目而不仅仅是日期?我希望我至少在正确的轨道上

    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)
演示:


使用jQuery跨浏览器

future = $.grep(future, function(value){
    return new Date(value.year, $.inArray(array, value.month), value.day) > today;
})

演示:

您需要为日期的月份(0-11)确定一个基于0的值,然后可以从数组中选择过去的日期

var未来=[{
“月”:“三月”,
“天”:1,
“年度”:2014年
}, {
“月”:“五月”,
“天”:29,
“年份”:2020年
}],
现在=新日期(),
月份=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”],
getMonth=函数(monthString){
返回月份。指数(月字符串);
}
用于(未来的var i){
var date=新日期(未来[i].年、getMonth(未来[i].月)、未来[i].日),
isPast=日期<现在;
如果(isPast){
未来。拼接(i,1);
}
}
console.log(未来);

您可以将每个日期转换为一个字符串,通过Date.parse可以轻松解析该字符串

var today = new Date();
today.setHours(0,0,0,0);

future = future.filter(function(date) {
    var parsableDate = date.month + ' ' + date.day + ', ' + date.year;
    return Date.parse(parsableDate) > today;
});

首先,您不需要创建月份数组,因为js Date支持“2014年3月1日”这样的格式。 我建议您将未来日期添加到另一个数组中

 var future =  [
     {
       "month":"march",
       "day":1,
       "year":2014
       },
     {
       "month":"may",
       "day":29,
       "year":2020
       }
    ];

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

Date
构造函数的参数在这里应该是数字。@杰克,我不这么认为。日期构造函数接受日期字符串。我用这个实现解决了我的问题。但是,如果用户所说的“比今天老”,则表示比昨晚午夜早。然后必须调整今天,因此,
today=newdate();今天。设定时间(0);今天。设定分钟(0);今天。设置秒(0);今天=今天。设置毫秒(0)和相应的其余代码。否则,这是“比现在老”的完美实现
 var future =  [
     {
       "month":"march",
       "day":1,
       "year":2014
       },
     {
       "month":"may",
       "day":29,
       "year":2020
       }
    ];

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