如果您查看我的日期验证,当我测试它是否在过去时,它是有效的,尽管日期构造函数需要一个零日期月份,那么我如何从表示月份的子字符串值中减去一(从结果中减去一,而不是从位置中减去一)
//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");
}发布于 2013-06-25 00:09:10
那么我怎么减去1呢?
使用subtraction operator "-"和数字文字"1“。它还具有之前将字符串转换为数字的优点。对于年份和日期,您可以使用一元加号显式地执行该转换(尽管Date构造函数隐式地执行该转换):
new Date(+courseYear, courseMonth-1, +courseDay);https://stackoverflow.com/questions/17279821
复制相似问题