C# .NET - Time Difference Calculation in Asp.Net

Asked By Kishore Rajendran on 08-Feb-11 11:07 PM
I created an ajax timer in a asp.net page. its showing current time,ie short time(9:33 AM)

and its running...................

When i clicked on a button,current clock time displays on a textbox, and on another button click the current time displays in another textbox.....

For example if the value in the first textbox is 9:23
and second textbox is 9:30

Ho can i calculate the time difference??

9:30 - 9:23 = 0:07

Help me to find the time difference
Mihir Soni replied to Kishore Rajendran on 08-Feb-11 11:18 PM

Hello,

Good Morning.

Convert both textbox value into dateTime and then you will able to get difference.

DateTime startTime = DateTime.Now;

DateTime endTime = DateTime.Now.AddSeconds( 75 );

TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );

Subhashini Janakiraman replied to Kishore Rajendran on 08-Feb-11 11:39 PM
For Results you can use the following code,

TimeSpan diff = Convert.ToDateTime(TextBox2.Text).TimeOfDay-Convert.ToDateTime(TextBox1.Text).TimeOfDay;

Response.Write(diff.ToString());