0

如果您在我来测试它是否在过去时查看我的日期验证,尽管日期构造函数期望一个零日期月份,但它可以工作,那么我如何从表示月份的子字符串值中减去一个(从结果中减去一个,而不是位置)

//start of datefield
var dateformat=/^(?:(?:31\/(?:0[13578]|1[02])|(?:29|30)\/(?:0[13-9]|1[012])|(?:0[1-9]|1\d|2[0-8])\/(?:0[1-9]|1[0-2]))\/[2-9]\d{3}|29\/02\/(?:[2-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[3579][26])00))$/;
if (!date.match(dateformat))
{
      errors.push("format incorrect use dd/mm/yyyy make sure you are entering correct days to the month remember 30 days have september, april, june & november, only 28 days in february unless leap year next is 2016");

}
var today = new Date();

var courseYear =date.substr(6,4) // use substr or substring to capture the last four digits
var courseMonth =date.substr(3,2) // use substr or substring to capture the four and fifth digits
var courseDay = date.substr(0,2)//e the first and second digits

var dateToCompare = new Date(courseYear, courseMonth, courseDay);

if (dateToCompare < today) {
  errors.push("this date is in the past"); 

}
4

1 回答 1

3

那我怎么减一

使用减法运算符“ -和数字文字“ 1”。它还具有将字符串转换为数字的好处。对于年份和日期,您可以使用一元加号显式进行转换(尽管Date构造函数隐式执行):

new Date(+courseYear, courseMonth-1, +courseDay);
于 2013-06-24T16:09:10.167 回答