简体   繁体   English

日期格式为dd.MM.yyyy的正则表达式?

[英]Regex for Date in format dd.MM.yyyy?

Trying to create a regex for Date in this format dd.MM.yyyy . 尝试以dd.MM.yyyy格式为Date创建一个regex

I want to use it with a DataAnnotation , like this 我想将它与DataAnnotation一起使用

[RegularExpression(@"<theregex>")]
public DateTime Date { get; set; }

I suggest that regex is not exactly the best way to do this. 我建议正则表达式并非完全是实现此目的的最佳方法。 You haven't given the context, so it's hard to guess what you're doing overall... but you might want to create a DateExpression attribute or such and then do: 您尚未提供上下文,因此很难猜测您总体上在做什么...但是您可能想要创建一个DateExpression属性或类似的属性,然后执行以下操作:

return DateTime.ParseExact(value, "dd.MM.yyyy");

in wherever your converter is defined. 在定义转换器的任何地方。

^(0[1-9]|[12][0-9]|3[01])[.](0[1-9]|1[012])[.](19|20)[0-9]{2}$

此正则表达式匹配01.01.190001.01.2000 ,但不符合1.1.20001/1/00

[0-3]{0,1}[0-9]\.[0-1]{0,1}[0-9]\.[0-9]{4,2}

比赛: 28.2.961.11.200812.10.2005

Just because I always find this site useful, here is an online regex checker . 仅因为我总是觉得此站点有用,所以这里是一个在线正则表达式检查器 On the right hand side it also has examples and community contributions. 在右侧,它也有示例和社区贡献。 In there are a number of Date matching regex variations. 在其中有许多日期匹配的正则表达式变体。

You can type in a load of dates you wish to match and try some of the different examples if you're not sure which is best for you. 您可以输入想要匹配的日期,如果不确定哪个最适合您,可以尝试一些不同的示例。 As with anything, there are multiple ways to solve the problem, but it might help you choose the one that fits best. 与其他任何方法一样,有多种方法可以解决问题,但它可能会帮助您选择最合适的方法。

^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$ 

This one also accepts - and / as separators. 这个也接受-和/作为分隔符。 You can remove them if you want. 您可以根据需要删除它们。

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

相关问题 日期格式 dd.MM.yyyy 的正则表达式模式 - Regex pattern for date format dd.MM.yyyy 字符串到日期时间“hh:mm:ss”或“dd.mm.yyyy hh:mm:ss”格式 - string to date time “hh:mm:ss” or “dd.mm.yyyy hh:mm:ss” format 使用格式 &#39;dd.MM.yyyy HH:mm:ss.fff z&#39; 将字符串转换为日期 - Convert string to date with format 'dd.MM.yyyy HH:mm:ss.fff z' 转换日期时间以将“dd.MM.yyyy”从另一个时区格式化为+2 - Convert date time to format “dd.MM.yyyy” from another time zone to +2 如何将 Json 日期时间转换为普通日期时间格式(dd.mm.yyyy)? - How Can I Convert Json Datetime To Normal Date Time Format(dd.mm.yyyy)? 如何在excel中将日期转换为C#中的dd.MM.yyyy格式 - How to Convert date into dd.MM.yyyy format in C# from excel 如何解析dd.mm.yyyy格式的RegularExpressionAttribute MVC的DataAnnotations? - how to parse dd.mm.yyyy format RegularExpressionAttribute MVC'sDataAnnotations? c# SQL 查询错误,格式为 dd.MM.yyyy - c# SQL query Error with format dd.MM.yyyy 按日期部分分组(dd.mm.yyyy) - Group by date-part (dd.mm.yyyy) 将不带分隔符的纯字符串转换为格式为“dd.MM.yyyy HH:mm:ss.fff z”的日期 - Convert plain string without seperator to date with format 'dd.MM.yyyy HH:mm:ss.fff z'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM