在C#中如何将日期和时间组合到DateTime中 [英] In C# how to combine date and time into DateTime
本文介绍了在C#中如何将日期和时间组合到DateTime中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我正在设计一个表单,其中我在两个不同的日期时间选择器中从用户那里获取日期和时间,现在我想将该日期时间组合到datetime的单个对象中,您能帮助我。
感谢adv。
解决方案
尝试
var date = new DateTime (X,Y,Z); // x,y,z是year.month和dateell
var time = TimeSpan.Parse(time);
var combo = date + time;
当您使用DateTimePicker时,设置一个格式以便它只允许用户输入有限的信息,例如Date in one和Time in another,结果值包括用户无法更改的条目的当前系统日期/时间。因此,将两者结合起来的唯一安全方法是创建一个新的DateTime值:
DateTime d = dtpDate.Value;
DateTime t = dtpTime.Value;
DateTime dtCombined = new DateTime(d.Year,d.Month,d.Day,t.Hour,t.Minute,t.Second);
试试这个...
DateTime.Date.Add(DateTime.TimeOfDay);
Hi i am designing a form in which i am taking date and time from user in two different datetime picker and now i want to combine that datetime into a single object of datetime can you help me.
Thanks in advn.
解决方案
Try
var date = new DateTime(x,y,z); // x,y,z are year.month and date respectivel var time = TimeSpan.Parse(time); var combo = date + time;
When you use a DateTimePicker, and set a format so that it only lets the user enter limited info, such as the Date in one, and the Time in another, the resulting values include the current system date/time for the entries the user cannot change. As a result the only safe way to combine the two is by creating a new DateTime value:
DateTime d = dtpDate.Value; DateTime t = dtpTime.Value; DateTime dtCombined = new DateTime(d.Year, d.Month, d.Day, t.Hour, t.Minute, t.Second);
Try This...
DateTime.Date.Add(DateTime.TimeOfDay);
这篇关于在C#中如何将日期和时间组合到DateTime中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文