使用 JavaScript 进行日期验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10523977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Date validation with JavaScript
提问by WordToTheWise
I have a date string in this format - "DD-MM-YYYY" this validates that successfully:
我有一个这种格式的日期字符串 - “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;
I need to check that the inserted date is after today's date(or today's date). how can i do that with JavaScript?
我需要检查插入的日期是否在今天的日期(或今天的日期)之后。我怎么能用 JavaScript 做到这一点?
I've tried this: http://www.redips.net/javascript/date-validation/with the separator, didn't work. suggestions?
我试过这个:http: //www.redips.net/javascript/date-validation/带分隔符,没有用。建议?
回答by ricardordz
First, this is your current date in javascript:
首先,这是您在 javascript 中的当前日期:
var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1; // Zero indexed
All you need to do, from here, is to compare this with your start date!
从这里开始,您需要做的就是将其与您的开始日期进行比较!
Best regards!
最好的祝福!
check this outmaybe it helps to understand the date object.
看看这个也许有助于理解日期对象。
回答by Hristo
Check out date.js, specifically...
查看date.js,特别是...
http://code.google.com/p/datejs/wiki/APIDocumentation#compare
http://code.google.com/p/datejs/wiki/APIDocumentation#compare
Compares the first date to the second date and returns an number indication of their relative values. -1 = this is < date. 0 = values are equal. 1 = this is > date.
将第一个日期与第二个日期进行比较,并返回其相对值的数字指示。-1 = 这是 < 日期。0 = 值相等。1 = 这是 > 日期。
The isAfter()
and the isBefore()
methods might be useful for your problem :)
该isAfter()
和isBefore()
方法可能是你的问题非常有用:)
Download the library here:
在此处下载库:
http://code.google.com/p/datejs/downloads/detail?name=date.js&can=2&q=
http://code.google.com/p/datejs/downloads/detail?name=date.js&can=2&q=
Also, its worth mentioning to checkout moment.js. I think the two libraries complement each other.
此外,值得一提的是结帐moment.js。我认为这两个库是相辅相成的。
回答by timrwood
You could do this with moment.jspretty easily.
你可以很容易地用moment.js做到这一点。
var input = moment(startDate, "DD-MM-YYYY");
if (input < moment()) {
// before today
} else {
// after today
}
We're also adding date validation pretty soon. See more info about validation here: https://github.com/timrwood/moment/pull/306
我们也很快就会添加日期验证。在此处查看有关验证的更多信息:https: //github.com/timrwood/moment/pull/306
回答by pXel
You should check each date getTime()
method and compare it. It's plain and simple, you don't need additional frameworks.
您应该检查每个日期getTime()
方法并进行比较。它简单明了,您不需要额外的框架。
Here is an example that parses the dates from the strings, and then compares them:
下面是一个从字符串解析日期,然后比较它们的示例:
var todayDate = "10-05-2012";? // A sample date
var compareDate1 = "10-05-2012";
var compareDate2 = "03-05-2012";
var compareDate3 = "10-07-2012";
compareDates(todayDate, compareDate1);
compareDates(todayDate, compareDate2);
compareDates(todayDate, compareDate3);
function compareDates(date1String, date2String) {
var date1 = parseDate(date1String);
var date2 = parseDate(date2String);
if(date1.getTime() > date2.getTime()) {
alert("First date(" + date1String + ") is older than second date(" + date2String + ").");
} else if(date1.getTime() < date2.getTime()) {
alert("First date(" + date1String + ") is younger than second date(" + date2String + ").");
} else {
alert("The dates are the same day");
}
}
function parseDate(stringDateParam) {
var parsedDay = parseInt(stringDateParam.substring(0,2));
var parsedMonth = parseInt(stringDateParam.substring(3,5))-1;
var parsedYear = parseInt(stringDateParam.substring(6,10));
var parsedDate = new Date(parsedYear, parsedMonth, parsedDay, 0 , 0, 0, 0);
return parsedDate;
}
?
// Output:
//
// First check: The dates are the same day
// Second check: First date(10-05-2012) is older than second date(03-05-2012).
// Third check: First date(10-05-2012) is younger than second date(10-07-2012).
You probably already have a function that parses string to date object, and you should implement a check similar to the one in function compareDates
based on getTime()
function.
您可能已经有一个将字符串解析为日期对象的函数,您应该实现一个类似于compareDates
基于getTime()
函数的函数中的检查。
If you have further questions, leave a comment. Good Luck!
如果您还有其他问题,请发表评论。祝你好运!
JSFiddle working example: click here
JSFiddle 工作示例:单击此处
回答by Michael Mior
Something like this should work. Could use some cleanup, but hopefully gets the point across.
像这样的事情应该有效。可以使用一些清理,但希望能理解这一点。
var dateFormat = /(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-(\d{4})/;
var dateMatch = startDate.exec(dateFormat);
var today = new Date();
today.setHours(0); today.setMinutes(0); today.setSeconds(0); today.setMilliseconds(0);
if ((new Date(dateMatch[3], dateMatch[2] - 1, dateMatch[1])).getTime() >= today.getTime()) {
// Date is after or on today
}
回答by WordToTheWise
Thank you all! this did the trick:
谢谢你们!这做到了:
var today = new Date();
var Tday = today.getDate();
var Tmonth = today.getMonth()+1; // Zero indexed
var Tyear = today.getFullYear();
var aoDate;
var separator= '-';
aoDate = startDate.split(separator);
var month = aoDate[1] - 0;
var day = aoDate[0] - 0;
var year = aoDate[2] - 0;
if(year < Tyear){
alert("'Start Date' must be today or after today!");
return false;
}
if((year == Tyear) && (month < Tmonth)){
alert("'Start Date' must be today or after today!");
return false;
}
if((year == Tyear) && (month == Tmonth) && (day < Tday)){
alert("'Start Date' must be today or after today!");
return false;
}