添加时间在C#中的DateTime [英] Adding a Time to a DateTime in C#

查看:124
本文介绍了添加时间在C#中的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日历和包含一天中的时间的文本框。我想创建一个日期,它是两者的结合。我知道我可以通过查看时间和mintues,然后将这些日历日期时间做到这一点,但这似乎相当混乱。

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 mintues and then adding these to the calendar DateTime, but this seems rather messy.

有没有更好的办法?

推荐答案

您可以使用的 DateTime.Add ()方法添加的时间日期。

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:dddd}", combined);

您还可以通过的parsing一个String 的,如果这是你需要做的。

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

另外,你可以看看使用其他控件。如果你正在使用的WinForms,WPF或asp.net你没有提到,但也有支持的日期和时间选择不同的日期和时间选择器控件。

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.

这篇关于添加时间在C#中的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆