We are getting some data from feed where date is something like "28-07-2020 09:11:57 AM" when i am trying to convert it it is always giving error that
"String was not recognized as a valid DateTime."
I have used many methods like Convert.ToDateTime, DateTIme.Parse , DateTime.parseExact but all are having same error.
string s = "28-07-2020 09:09:57 AM"; var dt= DateTime.ParseExact(s,"M/d/yyyy h:mm:ss tt",CultureInfo.InvariantCulture);
DateTime dt2= Convert.ToDateTime(s);
DateTime dt3=DateTime.Parse(s);
Can someone please suggest what is the issue in date format. We need to change it regardless culture as it is a window application and running on diff machines.
DateTime.ParseExact
with a pattern that matches this format. Are you sure the data shouldn't be reversed? The de-facto standard for date literals (ie strings) is the ISO8601 format. I'd expect any feed or HTTP API to return dates as2020-07-28 09:11:57
, possibly with a timezone offset – Panagiotis Kanavos