使用JavaScript验证日期

Wor*_*ise 0 html javascript validation

我有一个这种格式的日期字符串 - "DD-MM-YYYY"这成功验证:

var dateFormat = /(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-\d{4}/ ;
if(!startDate.match(dateFormat)){
    alert("'Start Date' must be in format: DD-MM-YYYY");
    return false;
Run Code Online (Sandbox Code Playgroud)

我需要检查插入日期是否在今天的日期之后(或今天的日期).我怎么能用JavaScript做到这一点?

我试过这个:http: //www.redips.net/javascript/date-validation/ 与分隔符,没有用.建议?

小智 5

首先,这是您在javascript中的当前日期:

var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1; // Zero indexed
Run Code Online (Sandbox Code Playgroud)

从这里你需要做的就是将它与你的开始日期进行比较!

最好的祝福!

检查这个可能有助于理解日期对象.