出生日期应为DD-MM-YYYY格式,出生日期不应为 [英] Date of Birth should be in DD-MM-YYYY format and date of birth should not be
本文介绍了出生日期应为DD-MM-YYYY格式,出生日期不应为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用正则表达式验证DateOfBirth。
哪个完整填写以下条件
出生日期应该是DD-MM-YYYY格式(如12-12-2014等),出生日期不应该是未来日期
任何人请帮助我
Hi,
I need to validate the DateOfBirth using regular Expression.
Which full fill the below condition together
Date of Birth should be in DD-MM-YYYY format (like 12-12-2014 etc) and date of birth should not be future date
Any one please help me
推荐答案
不要尝试使用正则表达式:它们是文本处理器,并且正确验证日期是一个真正的痛苦:例如,想想闰年。
相反,使用DateTime.TryParseExact - 如果它工作,它甚至会为您提供检查日期在范围内所需的数据:
Don't try to use a Regex: they are Text processors, and to validate a date correctly is a real pain: think about leap years for example.
Instead, use DateTime.TryParseExact - if it works it even gives you the data you need to check the date is in range:
DateTime dtBirth;
if (DateTime.TryParseExact(input, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtBirth))
{
if (dtBirth < DateTime.Now.Date)
{
...
}
}
string input ="12-12-2014";
DateTime dt;
if (DateTime.TryParseExact( input, "dd-MM-yyyy",CultureInfo.InvariantCulture,DateTimeStyles.None, out dt)
&& dt < DateTime.Now)
{
//validation success
}
日期是日期,没有别的,无论是什么显示格式!
您不需要任何正则表达式。您需要将文本转换为 DateTime [ ^ 。请参阅Solution1或Solution2。
Date is date and nothing else, no matter of displayed format!
You don't need any regular expression. You need to convert text into DateTime[^]. Please, see Solution1 or Solution2.
这篇关于出生日期应为DD-MM-YYYY格式,出生日期不应为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文