views:

72

answers:

2

Hello

How would a constraint for a route look like that needs to be in the format:yyyy-MM-dd hh: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

+1  A: 

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);
}
Svetlozar Angelov
Nothing to do with the question
Jan Jongboom
Using regex matching is the only way to create validation for your data? "How would a constraint for a route look like that needs to be in the format:yyyy-MM-dd hh:mm?"
Svetlozar Angelov
He wants to create an asp.net-mvc route, which is based on regex matching your url to redirect the request to a handler.
Jan Jongboom
+1  A: 

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.

Omar