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

后端 未结 9 1162
刺人心
刺人心 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:53

    use DateTime.Parse () to parse the date and the time separately. Then add the time component of the second one to the first one, like this

    var date = DateTime.Parse (one);
    var time = DateTime.Parse (two);
    var result = date + time - time.Date;
    

提交回复
热议问题