Calculateing Difference between Two Time

vishalneeraj-24503
Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 400
In some cases developers need to find out Time Difference or timediff method or timediff function to get the time between two Time.
Below is the code:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DateTime dtFrom;
DateTime dtTo;
string From = "10:35:00";
string To = "12:50:00";

if (DateTime.TryParse(From, out dtFrom) && DateTime.TryParse(To, out dtTo))
{
TimeSpan tSpan = dtTo - dtFrom;
int nDay = tSpan.Days;
int nHour = tSpan.Hours;
int nMin = tSpan.Minutes;
int nSec = tSpan.Seconds;

string TimeDiff = nDay.ToString("00") + ":" + nHour.ToString("00") + ":" + nMin.ToString("00") + ":" + nSec.ToString("00");
Response.Write(TimeDiff);
}
}
}

Output:-
00:02:15:00

Comments or Responses

Login to post response