function mainFunc() {
dueDate = "30/12/2014";
var firstReminderDate = dueDate;
var today = new Date();
var firstDate = convertToDate(firstReminderDate);
if (today > firstDate) {
//send reminder to A
} else {
// send reminder to B
}
}
function convertToDate(dateString) {
var dateData = dateString.split("/");
var date = new Date(new Date().setFullYear(dateData[0], dateData[1] - 1, dateData[2]));
return new Date(date);
}
我需要比较两个日期而不是时间,以及如何删除时间部分并仅比较日期?
convertToDate()是否每次都会返回“Thu Jan 01 05:30:00 GMT + 05:30 1970”?
最佳答案
您可以简化代码。要从dd/mm/yyyy
获取日期,只需将/
拆分,反转结果并将其加入'/'即可得到yyyy/mm/dd
,这是新Date
与其他Date
进行比较的有效输入。见片段
var report = document.querySelector('#result');
report.innerHTML += '30/12/2014 => '+ mainFunc('30/12/2014');
report.innerHTML += '<br>01/12/2014 => '+ mainFunc('01/01/2014');
function mainFunc(due) {
due = due ? convertToDate(due) : new Date;
return new Date > due
? due +' passed: <b>send reminder to A</b>'
: due +' not passed: <b>send reminder to B</b>';
}
function convertToDate(dateString) {
return new Date(dateString.split("/").reverse().join('/'));
}
<div id="result"></div>