Lasse Edsvik
Lasse Edsvik

Reputation: 9298

DateTime constraint on route with time?

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

Upvotes: 0

Views: 853

Answers (2)

JOBG
JOBG

Reputation: 4624

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.

Upvotes: 1

Svetlozar Angelov
Svetlozar Angelov

Reputation: 21680

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);
}

Upvotes: 1

Related Questions