我有一个函数,可以获取今天的日期并将其与两周后的日期进行比较。当它小于或等于当前日期时,应该添加一个类。但是,如果是今天的日期,则不起作用。否则它工作正常。有任何想法吗?谢谢
publicMethods.campaignEndDateAlert = function (dateString) {
if (!dateString) {
return dateString
};
var currentDate = new Date ();
var twoWeeks = new Date ();
twoWeeks.setDate(currentDate.getDate() + 14);
var inputDate = new Date(dateString);
// This is the part that doesn't seem to work - the first part of this if statement
if ((inputDate >= currentDate) && (inputDate <= twoWeeks)) {
dateString = '<span class="red">' + ax.Utils.RFCFormat(dateString, { excludeTime: true }) + '</span>';
} else {
dateString = ax.Utils.RFCFormat(dateString, { excludeTime: true })
};
return dateString;
};
最佳答案
根据您提供的信息:
您说过,如果是今天的日期,则无法按预期工作。这是因为 new Date()
将提供一个包含今天日期和时间的日期对象。 如果 dateString 的值类似于“07-13-2017”
没有时间,则需要从如果您希望 inputDate >= currentDate
为 true,则 currentDate
对象。在与 inputDate 进行比较之前,请尝试使用 currentDate.setHours(0, 0, 0, 0);
。
关于javascript - 与今天的日期相比,日期对象似乎不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45084601/