简体   繁体   English

YYYYMMDD日期格式化正则表达式以验证C#.net中的日期

[英]YYYYMMDD Date Format regular Expression to validate a date in C# .net

I need to validate the date format using regular expression in C#. 我需要在C#中使用正则表达式验证日期格式。

Here is the format: "YYYYMMDD" 格式如下: "YYYYMMDD"

Regular expressions aren't suitable for this task. 正则表达式不适合此任务。 It is difficult for example to write a regular expression that matches the valid date "20080229" but not the invalid date "20100229". 例如,很难编写与有效日期“20080229”匹配但不是无效日期“20100229”的正则表达式。

Instead you should use DateTime.TryParseExact with the format string "yyyyMMdd" . 相反,您应该使用DateTime.TryParseExact格式字符串"yyyyMMdd" Here is an example: 这是一个例子:

string s = "20100229";
DateTime result;
if (!DateTime.TryParseExact(
     s,
     "yyyyMMdd",
     CultureInfo.InvariantCulture,
     DateTimeStyles.AssumeUniversal,
     out result))
{
    Console.WriteLine("Invalid date entered.");
};

Ok, this is the most beautiful regex I've ever built. 好吧,这是我建造的最漂亮的正则表达式。 This accounts for all leap years since 1582, the introduction of leap. 这说明了自1582年以来所有闰年的飞跃。 It also handles non leap every fourth century. 它还处理每四个世纪的非飞跃。 For example, 1600 is leap but 1700 is not though it is divisible by four. 例如,1600是飞跃,但1700不是可以被4整除。 I tested this on all days between 1582 and 9999. 我在1582年到9999之间的所有日子都测试了这个。

var yyyymmdd = new RegExp("^(?:(?:(?:(?:(?:[13579][26]|[2468][048])00)|(?:[0-9]{2}(?:(?:[13579][26])|(?:[2468][048]|0[48]))))(?:(?:(?:09|04|06|11)(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02(?:0[1-9]|1[0-9]|2[0-9]))))|(?:[0-9]{4}(?:(?:(?:09|04|06|11)(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02(?:[01][0-9]|2[0-8])))))$");

Another version using dashes 另一个使用短划线的版本

var yyyyDashMmDashDd = new RegExp("^(?:(?:(?:(?:(?:[13579][26]|[2468][048])00)|(?:[0-9]{2}(?:(?:[13579][26])|(?:[2468][048]|0[48]))))-(?:(?:(?:09|04|06|11)-(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)-(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02-(?:0[1-9]|1[0-9]|2[0-9]))))|(?:[0-9]{4}-(?:(?:(?:09|04|06|11)-(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)-(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02-(?:[01][0-9]|2[0-8])))))$");

I love that this works for nearly all languages, as long as they support regex. 我喜欢它几乎适用于所有语言,只要它们支持正则表达式。 While its arguably more sensible to use a language specific date parser, I thinks this shows the power and elegance of regular expressions. 虽然使用特定语言的日期解析器可能更明智,但我认为这显示了正则表达式的强大和优雅。

Here is an image of the pattern with slashes in Regexper if you want to see it. 如果你想看到它,下面是Regexper中带有斜杠的图案的图像

Consider using DateTime.TryParseExact to validate the date. 考虑使用DateTime.TryParseExact来验证日期。 You can use the method to silmultaneously validate and read the DateTime value. 您可以使用该方法同时验证和读取DateTime值。

For example: 例如:

DateTime dateValue;
if (DateTime.TryParseExact(dateString, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
{
 //Parsed Successfully   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM