Vinz' Blog

"Code, Beer and Music" ~ my way of being a programmer!
posts - 123, comments - 362, trackbacks - 0

My Links

News

Archives

Image Galleries

Get the Difference Between Two Time in ASP.NET

Here's an example (One Way) on how are we going to calculate the time difference between two give time:

C#

 

        DateTime dFrom;
DateTime dTo;
string sDateFrom = "11:56:00";
string sDateTo = "12:12:00";
if (DateTime.TryParse(sDateFrom, out dFrom) && DateTime.TryParse(sDateTo, out dTo))
{
TimeSpan TS = dTo - dFrom;
int hour = TS.Hours;
int mins = TS.Minutes;
int secs = TS.Seconds;
string timeDiff = hour.ToString("00") + ":" + mins.ToString("00") + ":" + secs.ToString("00");
Response.Write(timeDiff); //output 16 mins in format 00:16:00
}
  

 VB.NET

 

Dim dFrom As DateTime
Dim dTo As DateTime
Dim sDateFrom As String = "11:56:00"
Dim sDateTo As String = "12:12:00"
If DateTime.TryParse(sDateFrom, dFrom) AndAlso DateTime.TryParse(sDateTo, dTo) Then
Dim
TS As TimeSpan = dTo - dFrom
Dim hour As Integer = TS.Hours
Dim mins As Integer = TS.Minutes
Dim secs As Integer = TS.Seconds
Dim timeDiff As String = ((hour.ToString("00") & ":") + mins.ToString("00") & ":") + secs.ToString("00")
Response.Write(timeDiff)'output 16 mins in format 00:16:00
End If

Print | posted on Tuesday, February 17, 2009 3:08 PM |

Feedback

Gravatar

# re: Get the Difference Between Two Time in ASP.NET

Too much code - and you're concatenating.

Would be much clearer to read with

string timeDiff = string.Format("{0:00}:{1:00}:{2:00}".
ts.Hours, ts.Minutes, ts.Seconds);

3/15/2009 10:45 PM | Tom
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: