Call the DateTime.ParseExact
overload, which takes an array of possible formats:
DateTime dt = DateTime.ParseExact(s, new[] { "MM/dd/yyyy h:mmt", "MM/dd/yy h:mmt" }, null, 0);
For the third argument, pass null
or DateTimeFormatInfo.CurrentInfo
if your date string is localized to the current user culture; pass DateTimeFormatInfo.InvariantInfo
if your date string is always in US format.
For the fourth argument, 0 is equivalent to DateTimeStyles.None
.
See documentation MSDN documentation .
source share