0

I am facing an issue in the parsing of the date. I have made following common function.

public static string ConvertedDate(string date)
        {
            if(!string.IsNullOrEmpty(date))
            {
                DateTime returnValue;
                bool flag = DateTime.TryParseExact(date, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out returnValue);
                return flag ? returnValue.ToString("MM.dd.yyyy") : null;
            }
            else
            {
                return null;
            }
        }

But very strange thing happens some strings are successfully converted into DateTime but some return false.

For example :

"2017-05-11 12:00:24" this string parse successfully.

"2015-03-06 20:18:42" this string can not.

The format of the string is same.

I observed that when "hour(hh)" goes above 12 then it cannot be parsed.

RMR
  • 599
  • 3
  • 12
  • 35

1 Answers1

7

You need to change yyyy-MM-dd hh:mm:ss to yyyy-MM-dd HH:mm:ss, which is hours in 24 hour time. Note the change from hh to HH.

See here: C# DateTime to "YYYYMMDDHHMMSS" format

Greg the Incredulous
  • 1,676
  • 4
  • 29
  • 42