19000101到20001231之间的日期的正则表达式
本文关键字:日期 正则表达式 之间 20001231 19000101 | 更新日期: 2023-09-27 17:53:39
谁能建议以下日期范围的REGX
格式为CCYYMMDD19000101 ~ 20001231空白
我是REGX的新手,请帮助我。
Regex解析日期时间??一位智者曾经说过:
有些人在遇到问题时,会想:
我知道,我将使用正则表达式
现在他们有两个问题。
来吧,你有内置的方法来完成这样的任务,比如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");
}
好了,现在您已经使用了前面提到的方法来解析日期,您可以简单地测试这个日期是否在预期范围内:
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
}