显示JavaScript截止日期变量是过去,未来还是今天到期?

时间:2015-05-27 07:48:47

标签: javascript jquery datetime

我希望根据Due Date past duein the futuredue today

在我的JavaScript应用中显示一些不同的数据

以下代码非常适合展示Due Date是将来还是过去的今天。

它不适用于显示截止日期是否设置为今天的日期,所以我可以使用一些帮助让这部分工作吗?

http://jsfiddle.net/jasondavis/sfj691st/

// Today's Date
var todayDate = new Date();

// Future Due Date
var futureDueDate = new Date("08/07/2015");

// Past Due Date
var pastDueDate = new Date("03/29/2015");

// Due Date Due Today!
var dueDateToday = new Date("05/27/2015");


// selected dueDate is in the future
if (futureDueDate < todayDate) {
    alert('Due date is past due from todays date!');
}else if (futureDueDate == todayDate) {
    alert('Task Due date is due today!');
}else{
    alert('Due date is in the future from todays date!');
}


// selected dueDate is in the past
if (pastDueDate < todayDate) {
    alert('Due date is past due from todays date!');
}else if (pastDueDate == todayDate) {
    alert('Task Due date is due today!');
}else{
    alert('Due date is in the future from todays date!');
}


// selected dueDate is in the past
if (dueDateToday < todayDate) {
    alert('Due date is past due from todays date!');
}else if (dueDateToday == todayDate) {
    alert('Task Due date is due today!');
}else{
    alert('Due date is in the future from todays date!');
}

最终目标是获取Due DateToday's Date并更改我的DateTime相对时间字符串文字的颜色,以显示今天到期和过期的红色文字截止日期。显示将来设置的到期日期的绿色文本。

就像下面的图片一样:

如果截止日期是今天的未来日期

,则显示绿色文本

enter image description here

如果截止日期为过去日期或今日为时显示红色文字

enter image description here

1 个答案:

答案 0 :(得分:1)

if (dueDate.toDateString() == todayDate.toDateString()) {
    alert('Task Due date is due today!');
}

请参阅What is the best way to determine if a date is today in JavaScript?