Hi nauna,
The code is working correctly.
Check the below example.
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Label ID="lblTime" Text="text" runat="server" />
<asp:Timer ID="timer" runat="server" Interval="1000">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<div id="dvScroll" style="overflow-y: scroll; height: 260px; width: 200px">
1. This is a sample text<br />
2. This is a sample text<br />
3. This is a sample text<br />
4. This is a sample text<br />
5. This is a sample text<br />
6. This is a sample text<br />
7. This is a sample text<br />
8. This is a sample text<br />
9. This is a sample text<br />
10. This is a sample text<br />
11. This is a sample text<br />
12. This is a sample text<br />
13. This is a sample text<br />
14. This is a sample text<br />
15. This is a sample text<br />
16. This is a sample text<br />
17. This is a sample text<br />
18. This is a sample text<br />
19. This is a sample text<br />
20. This is a sample text<br />
21. This is a sample text<br />
22. This is a sample text<br />
23. This is a sample text<br />
24. This is a sample text<br />
25. This is a sample text<br />
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("EndDate", typeof(DateTime));
dt.Rows.Add("2018-09-28 17:00:34.943");
DateTime startDate = DateTime.Now;
DateTime endDate = Convert.ToDateTime(dt.Rows[0]["EndDate"].ToString());
lblTime.Text = CalculateTimeDifference(startDate, endDate);
}
public string CalculateTimeDifference(DateTime startDate, DateTime endDate)
{
int days = 0; int hours = 0; int mins = 0; int secs = 0;
string final = string.Empty;
if (endDate > startDate)
{
days = (endDate - startDate).Days;
hours = (endDate - startDate).Hours;
mins = (endDate - startDate).Minutes;
secs = (endDate - startDate).Seconds;
final = string.Format("{0} days {1} hours {2} mins {3} secs", days, hours, mins, secs.ToString().PadLeft(2, '0'));
}
return final;
}
Screenshot