Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Pls some one tell that how to get difference between start time and end time in seconds in asp.netc# my code is

C#
Label5.Text = DateTime.Now.ToLongTimeString();
DateTime startTime, endTime;
startTime = Convert.ToDateTime(Label5.Text);
endTime = Convert.ToDateTime(Label5.Text);
var timeDiff = new TimeSpan(endTime.Ticks - startTime.Ticks);
Label6.Text = Convert.ToString(timeDiff);


pls tell how to take diffeence in seconds...
Posted
Updated 14-Aug-12 1:30am
v2

C#
TimeSpan t =  endTime - startTime   ;
             Label6.Text = Convert.Tostring(t.Seconds);
 
Share this answer
 
v2
DateTime startTime, endTime;

startTime = Convert.ToDateTime(DateTime.Now.ToLongTimeString());

endTime = Convert.ToDateTime(DateTime.Now.ToLongTimeString());

var timeDiff = new TimeSpan(endTime.Ticks - startTime.Ticks).Seconds
MessageBox.Show(Convert.ToString(timeDiff));

use the property .Seconds you will get the desired!
 
Share this answer
 
There are two properties for the difference in Seconds. Please see this code for an example:
C#
private void GetTimeDiff()
{
  DateTime startTime = DateTime.Parse("08:30:18");
  DateTime endTime = DateTime.Parse("17:15:19");
  TimeSpan t = endTime - startTime;
  MessageBox.Show(t.Seconds.ToString());       //shows "1"
  MessageBox.Show(t.TotalSeconds.ToString());  //shows "3501"
}

Depending on what you want, you may use the properties "Seconds" or "TotalSeconds".
 
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