【发布时间】:2018-11-28 15:09:57
【问题描述】:
以下是我的 JavaScript 代码,用于尝试创建一个用户无法在未来这么多月内预订的最大日期:
var x= 12;
var arriveDate = "28/11/2018"
var currentDate = new Date();
var a_date = new Date(arriveDate);
var max_month = currentDate.setMonth(currentDate.getMonth()+ x);
if (arriveDate === ""){
$("#arrive_date_error").html("Please don't leave this field blank");
}
else if (a_date < currentDate){
console.log("Please don't select a date in the past")
}
else if (a_date > max_month){
console.log("date in future")
}
无论我尝试什么月/日/年,最后一个 else if 似乎都不起作用。我决定使用 console.log(max_month) 查看它创建的月份并返回: 1574953488195 而不是正确的格式: 2019 年 11 月 28 日星期四 15:04:48 GMT+0000
我做错了什么,为什么在我尝试更改日期对象的月份时会更改格式?
【问题讨论】:
-
"28/11/2018" 不是 ECMA-262(或我知道的任何实现)支持的格式。这可能会导致日期无效,或日期为 2020 年 4 月 11 日。或其他。
标签: javascript validation date format