Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
for some reason i want to calculate time difference but it is in 24 hour format.

Example
21:15 And 3:20 Difference between these two is 6 hour 5 minutes

Same like 3:20 to 21:25 18 hour 5 min.

Could anyone help me.
Posted

You can just subtract your DateTime objects. No need for you to think in which way the DateTime instances were or will be formatted.
 
Share this answer
 
use this


string s = DateTime.ParseExact("2130", "HHmm", CultureInfo.CurrentCulture).ToString("hh:mm tt");
TimeSpan span = new TimeSpan(0, 2, 0, 30, 0);
s = DateTime.ParseExact("2130", "HHmm", CultureInfo.CurrentCulture).Subtract(span).ToString("hh:mm tt");
Response.Write(s);
 
Share this answer
 
Refer

DateTime dateTime1 = Convert.ToDateTime("9:30");
      DateTime dateTime2 = Convert.ToDateTime("18:30");
      TimeSpan timeSpan = dateTime1.Subtract(dateTime2);
      lblTime.Text = timeSpan.ToString();
 
Share this answer
 
Comments
bunty swapnil 12-Feb-14 1:17am    
In this case Getting time -09:00:00 it should be 09:00:00.

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