开发者

DateTime constraint on route with time?

开发者 https://www.devze.com 2022-12-14 02:11 出处:网络
How would a constraint for a route look like that needs to be in the format:yyyy-MM-dd h开发者_JAVA百科h:mm?

How would a constraint for a route look like that needs to be in the format:yyyy-MM-dd h开发者_JAVA百科h:mm?

especially with the space there?

I got @"\d{4}-\d{2}-\d{2}" so far, but not sure about the rest

How's it done?

/M


The easy way would be

@"\d{4}-\d{2}-\d{2} \d{2}:\d{2}"

But that wont guarantee that its indeed a Date Time Value, you will have to re-check post binding, maybe with Angelov answer.

The other thing to notice is that your URL will get an ugly %20 for the space.


You can ensure the format with the regex, but you probable want to ensure that the datetime is valid. You can try DateTime.TryParseExact

Something like that:

public static bool IsDateValid(string s)
{
    DateTime d;
    return DateTime.TryParseExact(s, "yyyy-MM-dd hh:mm",null,System.Globalization.DateTimeStyles.None,out d);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消