Convert string to Datetime dd/MM/yyyy hh:mm:ss tt

前端 未结 4 962
悲哀的现实
悲哀的现实 2021-01-03 05:52

How can I convert this 7/3/2015 12:40:02 PM to DateTime with format \"dd/MM/yyyy hh:mm:ss tt\" I have done like this:

BreackEndTime         


        
4条回答
  •  心在旅途
    2021-01-03 06:47

    This is the code that worked well in my case:

            Console.WriteLine("deal with regex datetime: ");
            string input = "11/24 5:41:00 AM";
            DateTime newDate;
            CultureInfo enUS = new CultureInfo("en-US");
            try
            {
                newDate = DateTime.ParseExact(input, "M/d h:mm:ss tt", CultureInfo.InvariantCulture);
                Console.WriteLine("parse result: " + newDate);
            }
            catch (Exception err)
            {
                Console.WriteLine("error parsing input string. date format is wrong or string chaged " + err);
            }
    

提交回复
热议问题