【发布时间】:2019-08-21 01:37:00
【问题描述】:
我在使用 Javascript 比较两个日期时得到了错误的结果。我在下面解释我的代码。
var user_date='01-04-2019';
var todayDate = new Date();
var todayMonth = todayDate.getMonth() + 1;
var todayDay = todayDate.getDate();
var todayYear = todayDate.getFullYear();
if (todayDay < 10) {
todayDay = '0' + todayDay;
}
if (todayMonth < 10) {
todayMonth = '0' + todayMonth;
}
var todayDateText = todayDay + "-" + todayMonth + "-" + todayYear;
var inputToDate = Date.parse(user_date);
var todayToDate = Date.parse(todayDateText);
console.log(todayDateText);
//console.log(mydate);
if (inputToDate > todayToDate) {
alert("the input is later than today");
}else{
alert("the input is earlier than today");
}
在这里我收到else part 警报消息,其中用户输入的日期晚于今天的日期。
【问题讨论】:
-
if(new Date('01-04-2019').getTime() > new Date().getTime()) { //日期大于今天的日期 } else { //日期小于比今天的日期 }
标签: javascript date