Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 19-Jan-17 20:49pm

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;
 
Share this answer
 
v2
Comments
Code 89 14-Dec-11 4:13am    
+5!..
Reiss 14-Dec-11 4:15am    
Just wondered why you are using vars when you know explicitly what type you are getting?
Amir Mahfoozi 14-Dec-11 4:34am    
Though it is good programming style of him, it is a question of a lot of people : http://blog.dmbcllc.com/2009/09/07/csharp-var-misconceptions/ http://www.infoq.com/news/2008/05/CSharp-var
Govind K 14-Dec-11 7:33am    
Ok...
its fine but i am taking with datetime picker.
And http://www.codeproject.com/script/Membership/View.aspx?mid=6122202 has given correct answer and it works.
Thanks for respoce.
Abhinav S 14-Dec-11 10:07am    
You are welcome.
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:

C#
DateTime d = dtpDate.Value;
DateTime t = dtpTime.Value;
DateTime dtCombined = new DateTime(d.Year, d.Month, d.Day, t.Hour, t.Minute, t.Second);
 
Share this answer
 
Comments
Code 89 14-Dec-11 4:14am    
+5!..
Govind K 14-Dec-11 7:33am    
Once again thanks it works.
Emrul Kayes 20-Jun-15 5:28am    
Thank You.. Very Helpfull
Member 11052003 1-Aug-16 21:13pm    
Thank you very much.
Try This...

DateTime.Date.Add(DateTime.TimeOfDay);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900