在javascript中验证此"dd-MMM-yyyy"格式的两个日期

ACP*_*ACP 3 javascript validation date

我有两个日期18-Aug-201019-Aug-2010这种格式.如何查找哪个日期更大?

CMS*_*CMS 8

您需要创建自定义解析函数来处理所需的格式,并获取要比较的日期对象,例如:

function customParse(str) {
  var months = ['Jan','Feb','Mar','Apr','May','Jun',
                'Jul','Aug','Sep','Oct','Nov','Dec'],
      n = months.length, re = /(\d{2})-([a-z]{3})-(\d{4})/i, matches;

  while(n--) { months[months[n]]=n; } // map month names to their index :)

  matches = str.match(re); // extract date parts from string

  return new Date(matches[3], months[matches[2]], matches[1]);
}

customParse("18-Aug-2010");
// "Wed Aug 18 2010 00:00:00"

customParse("19-Aug-2010") > customParse("18-Aug-2010");
// true
Run Code Online (Sandbox Code Playgroud)