-1

I am wanting to show some different data in my JavaScript app based on a Due Date being past due, in the future, or due today

The code below works pretty good for showing if the Due Date is in the future or past due from today's date.

It does not work for showing if the due date is set for today's date though so I could use some help getting that part to work please?

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

The end goal is to take a Due Date and Today's Date and change the color of my DateTime relative time string of text to show red text for due today and past due Due Dates. SHow green text for Due Dates set in the future.

Just like my images below:

Show Green Text if the Due Date is a date in the Future from today

enter image description here

Show red text if the Due Date is PAST DUE or DUE TODAY

enter image description here

JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • Just a pedantic point but is `var futureDueDate = new Date("08/07/2015");` the 8th July or 7th August. Don't forget date invariance. – Paul Houlston May 27 '15 at 07:56

1 Answers1

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

See What is the best way to determine if a date is today in JavaScript?

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • Great thanks. Only thing I had to change was to move this as the first `If()` since a date matching todays date also matched for the `past due date`. Works good though thank you – JasonDavis May 27 '15 at 08:00