在C#中如何将日期和时间组合到DateTime中

编程入门 行业动态 更新时间:2024-10-23 14:33:02
本文介绍了在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中

本文发布于:2023-11-15 16:37:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   如何将   日期   时间   DateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!