I try to validate an input and then i can get input like that i want to.
Example:
if (string != validate(string))
then not valid
else
then valid
Inputs and expected output
2017-03-17T09:44:18.000+07:00 == valid
2017-03-17 09:44:18 == not valid
To check valid DateTime
, you need correct DateTime
format (i.e "yyyy-MM-ddTHH:mm:ss.fffzzz"
) and use DateTime.TryParseExact()
to validate your datetime string,
Try below code to validate your datetime string,
public void ValidateDateTimeString(string datetime)
{
DateTime result = new DateTime(); //If Parsing succeed, it will store date in result variable.
if(DateTime.TryParseExact(datetime, "yyyy-MM-ddTHH:mm:ss.fffzzz", CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
Console.WriteLine("Valid date String");
else
Console.WriteLine("Invalid date string");
}
Try it online
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With