Javascript 日期比较在jquery中无法正常工作

Javascript 日期比较在jquery中无法正常工作,javascript,jquery,Javascript,Jquery,如果某个日期距离当前日期不超过2周,我将尝试向div添加一个类。我的逻辑是这样的,但似乎不起作用。有什么想法吗?它似乎没有正确地进行比较(它将类添加到所有div中),我无法理解。谢谢你的帮助 function dateString () { var currentDate = new Date (); var twoWeeks = new Date (); twoWeeks.setDate(currentDate.getDate() + 14) var EndDa

如果某个日期距离当前日期不超过2周,我将尝试向div添加一个类。我的逻辑是这样的,但似乎不起作用。有什么想法吗?它似乎没有正确地进行比较(它将类添加到所有div中),我无法理解。谢谢你的帮助

function dateString () {
    var currentDate = new Date ();
    var twoWeeks = new Date ();
    twoWeeks.setDate(currentDate.getDate() + 14)
    var EndDateDiv = $('.end-date');


    if (currentDate <= twoWeeks) { 
        EndDateDiv.addClass('red');
  }
};
函数日期字符串(){
var currentDate=新日期();
var twoWeeks=新日期();
两周.setDate(currentDate.getDate()+14)
var EndDateDiv=$('.end date');
如果(currentDate我会做:

function dateString () {
    var currentDate = new Date();
    var twoWeeks = new Date();
    twoWeeks.setDate(twoWeeks.getDate() + 14);
    var limit = 15;
    var EndDateDiv = $('.end-date');
    var diff = Math.round((twoWeeks-currentDate)/(1000*60*60*24))
    if (diff > limit) { 
        EndDateDiv.addClass('red');
    }
};
工作小提琴==>

我会做:

function dateString () {
    var currentDate = new Date();
    var twoWeeks = new Date();
    twoWeeks.setDate(twoWeeks.getDate() + 14);
    var limit = 15;
    var EndDateDiv = $('.end-date');
    var diff = Math.round((twoWeeks-currentDate)/(1000*60*60*24))
    if (diff > limit) { 
        EndDateDiv.addClass('red');
    }
};

工作小提琴==>

它的工作方式与您预期的一样。请参阅以下代码片段

dateString();
函数日期字符串(){
var currentDate=新日期();
var twoWeeks=新日期();
两周.setDate(currentDate.getDate()+14)
var EndDateDiv=$('.end date');

如果(currentDate,它会按照您的预期工作。请参阅以下代码段

dateString();
函数日期字符串(){
var currentDate=新日期();
var twoWeeks=新日期();
两周.setDate(currentDate.getDate()+14)
var EndDateDiv=$('.end date');
if(currentDate
函数日期字符串(){
var currentDate=新日期();
var twoWeeks=新日期();
警报(两周);
var EndDateDiv=$('.end date');
if(currentDate.getTime()
函数日期字符串(){
var currentDate=新日期();
var twoWeeks=新日期();
警报(两周);
var EndDateDiv=$('.end date');

如果(currentDate.getTime()谢谢你,也许我会发疯(可能)没问题。有时会发生。谢谢你,也许我会发疯(可能)没问题。有时会发生。