如何通过在JavaScript中比较今天的格式化日期来检查将来的日期?下面的代码中注释了所需的变量。
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可能是明天后的任何一天,可能是明年或下个月。明天逻辑也需要更正,因为它在每月的最后一天不起作用。 Items [i] .showDate始终以yyyy-mm-dd格式的字符串形式出现。
答案 0 :(得分:1)
从此答案中采用:Adding hours to Javascript Date object?
理论是,通过在当前日期上加上所需的毫秒数,然后从结果中创建一个新日期-应该考虑月份/ leap年的天数
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));
}
答案 1 :(得分: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));
}