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

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

问题描述

我有一个日历和一个包含一天中的时间的文本框。我想创建一个这两个组合的datetime。我知道我可以通过查看几个小时和上午,然后将它们添加到日历DateTime中,但这似乎很麻烦。

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.

有更好的方法吗? / p>

Is there a better way?

推荐答案

您可以使用 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);

您还可以通过解析字符串,如果这是您需要做的。

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天全站免登陆