How to combine two strings (date and time) to a single DateTime

后端 未结 9 1095
刺人心
刺人心 2021-01-17 11:34

I have two strings:

string one = \"13/02/09\";
string two = \"2:35:10 PM\";

I want to combine these two together and convert to a Dat

9条回答
  •  不思量自难忘°
    2021-01-17 11:54

    Convert.ToDateTime uses DateTime.ParseExact with your current thread's culture, so you can make things a bit clearer by simply doing:

    string date = "13/02/09";
    string time = "2:35:10 PM";
    DateTime dateTime = DateTime.Parse(date +" "+ time, new CultureInfo("en-GB"));
    Console.WriteLine (dateTime);
    

    That gives the result 13/02/2009 14:35:10, and forces the parse to use the en-GB date time formats. If your Windows installation is en-GB anyway, you don't need the CultureInfo(..) argument.

提交回复
热议问题