【问题标题】:How to check future date by comparing today's formatted date in javaScript?如何通过比较 JavaScript 中今天的格式化日期来检查未来日期?
【发布时间】:2018-10-18 22:09:33
【问题描述】:

如何通过比较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 的字符串形式出现。

【问题讨论】:

  • 什么是futureDate?后天?下周的某个时间?下个月?明年?此外,您的“明天”逻辑存在问题:例如,如果日期是 2018 年 10 月 31 日,您最终会得到 2018-10-32,这是无效的。你应该看看Moment
  • futureDate 可能是后天的任何一天,可能是明年或下个月。关于明天的逻辑很好!也需要一个正确的逻辑。 Items[i].showDate 总是以这种格式 yyyy-mm-dd 的字符串形式出现。
  • 注意拼写 JavaScript。 javascriptisnotjava.io

标签: javascript


【解决方案1】:

改编自这个答案:Adding hours to Javascript Date object?

理论是通过将所需的毫秒数添加到当前日期,并根据结果创建一个新日期 - 应考虑月份/闰年的天数

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-03-27
      • 2018-10-01
      • 1970-01-01
      • 2023-03-13
      • 2015-11-28
      • 2017-05-02
      • 1970-01-01
      • 2011-09-26
      • 2014-11-03
      相关资源
      最近更新 更多