Reputation: 17
The following string needs to be validated: "04-01-2021 00:00:00 +00:00"
.
I've got the following method that checks if a string is a valid date, although it returns false for this string.
private static bool IsValidDate(string value)
{
var allowedFormats = new[]
{
"yyyy-MM-dd", "yyyy-MM-ddThh:mm:ss", "yyyy-MM-dd HH:mm:ss",
"dd-MM-yyyy", "dd-MM-yyyyTHH:mm:ss", "dd-MM-yyyy HH:mm:ss"
};
var cultureInfo = new CultureInfo("en-GB");
return DateTime.TryParseExact(value, allowedFormats, cultureInfo,
DateTimeStyles.AllowWhiteSpaces, out _);
}
Is there anything I'm missing from the allowedFormats
?
Thanks!
Upvotes: 1
Views: 190
Reputation: 690
It will work with "dd-MM-yyyy HH:mm:ss zzzzz"
format string.
zzzzz
part stands for offset (+00:00
) in your string.
Check out the Microsoft's documentation.
Upvotes: 3