Adding a Time to a DateTime in C#

后端 未结 6 1831
执笔经年
执笔经年 2020-12-05 06:32

I have a calendar and a textbox that contains a time of day. I want to create a datetime that is the combination of the two. I know I can do it by looking at the hours and m

6条回答
  •  攒了一身酷
    2020-12-05 06:55

    You can use the DateTime.Add() method to add the time to the date.

    DateTime date = DateTime.Now;
    TimeSpan time = new TimeSpan(36, 0, 0, 0);
    DateTime combined = date.Add(time);
    Console.WriteLine("{0:ffffdd}", combined);
    

    You can also create your timespan by parsing a String, if that is what you need to do.

    Alternatively, you could look at using other controls. You didn't mention if you are using winforms, wpf or asp.net, but there are various date and time picker controls that support selection of both date and time.

提交回复
热议问题