Click here to Skip to main content
15,885,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have been scouring the net in efforts to find how to add 2 DateTime fields. I simply want to recieve the total hours by adding 2 DateTime fields. Any help would be appreciated...
Thanks Again!
-Raj

Why wont this work...?

DateTime dteMon, dteTue;

System.DateTime answer = dteMon.Add(dteTue);
Posted
Comments
Toli Cuturicu 23-Sep-10 18:18pm    
You can not.

Looks like you want to subtract two DateTimes rather than add them. Subtracting gives a TimeSpan, which represents an interval. You can add TimeSpans.
Adding Monday 12:15 to Tuesday 09:45 doesn't make sense, but adding 1 day, 3 hours to 2 hours 45 minutes does make sense. Subtracting Monday 23:00 from Tuesday 07:15 does make sense, and should give 8 hours 15 minutes, which is a TimeSpan. You can add a DateTime and a TimeSpan to get another DateTime (Monday 18:00 + 11 hours gives Tuesday 05:00.)
Googling "subtract DateTime" goes straight to MSDN with all you need to know.

If you like this answer, vote for it, and mark it 'accepted'.
 
Share this answer
 
Thanks for helping understand this better guys! :)
 
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