Click here to Skip to main content
15,898,850 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i compare date & time, if more than 24 hours means it displays like example consider 25hours "1.01:00:00" 1 day plus 1 hour. i want result like this "25:00:00" hours.

Please help me...
Posted

1 solution

When you subtract two DateTime objects, you always get a TimeSpan object - this has the properties you need directly:
C#
DateTime now = DateTime.Now;
DateTime then = now.AddDays(-1).AddHours(-1);
TimeSpan diff = now - then;
Console.WriteLine("{0}:{1}:{2}", (int)diff.TotalHours, diff.Minutes, diff.Seconds);
DateTime random = new DateTime(2013, 2, 24, 7, 56, 23);
diff = now - random;
Console.WriteLine("{0}:{1}:{2}", (int)diff.TotalHours, diff.Minutes, diff.Seconds);
You need to cast the TotalHours to an int, as it returns the difference in time as a double, with the minutes and seconds as the fractional part.
 
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