How to validate the date and time in the url that is the correct DateTimeOffset format?
DateTimeOffset format (basically starts with 4 digits of year and end with uppercase Z):
- yyyy-mm-ddZ
- yyyy-mm-ddThh:mm:ssZ
- yyyy-mm-ddThh:mm:ss.fffffZ
Valid DateTimeOffset in the url:
- aaa.com/activityDateTime eq 2022-05-12Z
- aaa.com/activityDateTime eq 2022-05-12T12:00:00Z
- aaa.com/activityDateTime%20eq%202022-05-12T12:00:00Z
- aaa.com/activityDateTime+eq+2022-05-12T12:00:00.123456Z
- aaa.com/activityDateTime gt 2022-03-12Z le 2022-05-12Z
Note: eq means equal, gt means greater than, le means less than
Invalid DateTimeOffset in the url:
- aaa.com/activityDateTime eq 2022-05-12
- aaa.com/activityDateTime eq 2022-05-12T12:00:00
- aaa.com/activityDateTime%20eq%202022-05-12T12:00:00
- aaa.com/activityDateTime+eq+2022-05-12T12:00:00.123456
I'm currently convert the url to string and use Regex to validate it, but not sure what's the proper way to handle the case where url contains "%20". Is there a way not to use Replace()? Also, any other suggestions to validate DateTimeOffset in the url? Here's my current regex expression:
Regex checkDate = new Regex(@"^(?!\d{4}-\d{2}-\d{2}Z)");
Regex checkDateTime = new Regex(@"^(?!\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)");