I have a string 02/12/2022 06:53:43 and i want to parse it to a datetime to perform the following check
string myDate = "02/12/2022 06:53:43";
DateTime.Parse(myDate).AddMinutes(-2) < DateTime.UtcNow)
But it gives an error : String was not recognized as a valid DateTime.
How can i parse the date ?
>Solution :
Is this 12th of Feb or the 2nd of December? 😉
You could use ParseExact. There are plenty examples in DateTime.ParseExact Method.
Here something that works for your format, assuming it’s the 2nd of December.
var dateString = "02/12/2022 06:53:43";
var format = "dd/MM/yyyy hh:mm:ss";
var result = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);