正则表达日期在19000101到20001231之间

KCS*_*KCS -1 c#

任何机构可以建议REGX低于日期范围

格式为CCYYMMDD 19000101至20001231空白

我是REGX的新手,请帮帮我.

Dar*_*rov 5

正则表达式解析日期时间??? 一位智者曾经说过:

有些人在遇到问题时会想:

我知道,我会使用正则表达式.

现在他们有两个问题.

来吧,你有这样的任务的内置方法,如DateTime.TryParseExact:

string dateStr = "19000101";
DateTime date;
if (DateTime.TryParseExact(dateStr, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)
{
    // you could safely use the date instance created for you here
}
else
{
    throw new InvalidFormatException("Sorry the date you have given me is not in the expected format");
}
Run Code Online (Sandbox Code Playgroud)

好了,既然您已经使用上述方法来解析日期,那么您可以轻松地测试此日期是否在预期范围内:

DateTime start = new DateTime(1900, 1, 1);
DateTime end = new DateTime(2000, 12, 31);
DateTime date = ... use the previous method to parse your string
if (date > start && date < end)
{
    // success
}
else
{
    // the date is outside the range
}
Run Code Online (Sandbox Code Playgroud)