0

How to check future date by comparing today's formatted date in javaScript? The needed variable is commented in the below code.

var formattedDate = new Date();
var d = formattedDate.getDate();
var m =  formattedDate.getMonth();
m += 1;
var tomorrowDay = d + 1;
var y = formattedDate.getFullYear();
var todayDate = y + "-" + m + "-" + d;
var tomorrowDate = y + "-" + m + "-" + tomorrowDay;
var futureDate = ??? //Logic Needed

if (this.data) {
var Items = this.data.itemCollectionData;

for (var i = 0; i < dealItems.length; i++) {

if (Items[i].showDate == todayDate) {
Items[i].isToday = true;
} else if (Items[i].showDate == tomorrowDate) {
Items[i].isTomorrow = true;
} else if (Items[i].showDate == futureDate) {
Items[i].isFutureDate = true;
}
}
}

futureDate may be any day after tomorrow, it may be next year or next month. Tomorrow logic needs a correction too because it will not work on the last day of the month. Items[i].showDate always comes as a string in this format yyyy-mm-dd.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Chris Jake
  • 59
  • 1
  • 9
  • 2
    What is `futureDate`? The day after tomorrow? Some time next week? Next month? Next year? Also, there is a problem with your "tomorrow" logic: if the date is 31 October 2018, for example, you'll end up with `2018-10-32`, which isn't valid. You should look at [Moment](https://momentjs.com/). – Herohtar Oct 17 '18 at 20:52
  • 1
    Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Herohtar Oct 17 '18 at 20:57
  • futureDate may be any day after tomorrow, it may be next year or next month. Nice catch about the tomorrow logic! need a correct logic for that too. Items[i].showDate always comes as a string in this format yyyy-mm-dd. – Chris Jake Oct 17 '18 at 20:57
  • 1
    Take care in spelling JavaScript. http://javascriptisnotjava.io/ – Basil Bourque Oct 18 '18 at 00:20

2 Answers2

1

Adpated from this answer : Adding hours to Javascript Date object?

The theory is that by adding the milliseconds required onto the current date, and creating a new date from the result - days of the months / leap years should be accounted for

var numberOfDaysInFuture = 10;


var formattedDate = new Date();
var d = formattedDate.getDate();
var m = formattedDate.getMonth();
m += 1;
var y = formattedDate.getFullYear();
var todayDate = y + "-" + m + "-" + d;

// tomorrows +1 fix
var tomorrowDateObject = addDaysToDate(formattedDate, 1);
var tomorrowDate = tomorrowDateObject.getFullYear() + "-" + (tomorrowDateObject.getMonth() + 1) + "-" + tomorrowDateObject.getDate();

// create future date
var futureDateObject = addDaysToDate(formattedDate, numberOfDaysInFuture);
var futureDate = futureDateObject.getFullYear() + "-" + (futureDateObject.getMonth() + 1) + "-" + futureDateObject.getDate();

if (this.data) {
  var Items = this.data.itemCollectionData;

  for (var i = 0; i < dealItems.length; i++) {

    if (Items[i].showDate == todayDate) {
      Items[i].isToday = true;
    } else if (Items[i].showDate == tomorrowDate) {
      Items[i].isTomorrow = true;
    } else if (Items[i].showDate == futureDate) {
      Items[i].isFutureDate = true;
    }
  }
}

console.log("TODAY : " + todayDate)
console.log("TOMORROW : " + tomorrowDate)
console.log("FUTURE : " + futureDate);

// https://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object
function addDaysToDate(date, days) {
  // would be easy enough to change to years, days.. whatever you need
  return new Date(date.getTime() + (days * 24 * 60 * 60 * 1000));
}
Jiminibob
  • 26
  • 2
0

var numberOfDaysInFuture = 10;


var formattedDate = new Date();
var d = formattedDate.getDate();
var m = formattedDate.getMonth();
m += 1;
var y = formattedDate.getFullYear();
var todayDate = y + "-" + m + "-" + d;

// tomorrows +1 fix
var tomorrowDateObject = addDaysToDate(formattedDate, 1);
var tomorrowDate = tomorrowDateObject.getFullYear() + "-" + (tomorrowDateObject.getMonth() + 1) + "-" + tomorrowDateObject.getDate();

// create future date
var futureDateObject = addDaysToDate(formattedDate, numberOfDaysInFuture);
var futureDate = futureDateObject.getFullYear() + "-" + (futureDateObject.getMonth() + 1) + "-" + futureDateObject.getDate();

if (this.data) {
  var Items = this.data.itemCollectionData;

  for (var i = 0; i < dealItems.length; i++) {

    if (Items[i].showDate == todayDate) {
      Items[i].isToday = true;
    } else if (Items[i].showDate == tomorrowDate) {
      Items[i].isTomorrow = true;
    } else if (Items[i].showDate == futureDate) {
      Items[i].isFutureDate = true;
    }
  }
}

console.log("TODAY : " + todayDate)
console.log("TOMORROW : " + tomorrowDate)
console.log("FUTURE : " + futureDate);

// https://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object
function addDaysToDate(date, days) {
  // would be easy enough to change to years, days.. whatever you need
  return new Date(date.getTime() + (days * 24 * 60 * 60 * 1000));
}
Chris Jake
  • 59
  • 1
  • 9