C# String was not recognized as a valid DateTime [How to Solve]

Error reason: because the date string cannot be converted normally.

Resolution code:

class Program
    {
        static void Main(string[] args)
        {

            //1, the correct way to write (without the symbols of the date string to convert the common date format, formatDate output results are: 2021-10-11)
            IFormatProvider ifp = new CultureInfo("zh-CN", true);
            string formatDate = DateTime.ParseExact("20211011", "yyyyMMdd", ifp).ToString("yyyy-MM-dd");

            Console.WriteLine(formatDate);
            Console.ReadKey();
        }
    }

Similar Posts: