0

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

JOBG
  • 4,544
  • 4
  • 26
  • 47
Lasse Edsvik
  • 9,070
  • 16
  • 73
  • 109

2 Answers2

1

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
  • 21,214
  • 6
  • 62
  • 67
1

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.

JOBG
  • 4,544
  • 4
  • 26
  • 47