Working with Dates in ASP.NET

.NET makes working with dates incredibly easy. To find the difference between two dates, simply subtract them like subtracting two integers. The result of the operation is a timespan object.

Dim dt1 As DateTime = Now
Dim dt2 As DateTime = dt1.AddDays((New Random).NextDouble() * 2)
Dim ts As TimeSpan = dt2 - dt1
lblDates.InnerHtml = dt2 & " minus " & dt1
lblDays.InnerHtml = ts.Days
lblHours.InnerHtml = ts.Hours
lblMinutes.InnerHtml = ts.Minutes
lblSeconds.InnerHtml = ts.Seconds
lblMillisec.InnerHtml = ts.Milliseconds
lblTotalHours.InnerHtml = ts.Days * 24 + ts.Hours + ts.Minutes / 60
lblTotalMinutes.InnerHtml = ts.Days * 24 * 60 + ts.Hours * 60 + ts.Minutes

DATES: 07/29/2021 11:58:01 minus 07/29/2021 09:41:40
DAYS: 0
HOURS: 2
MINUTES: 16
SECONDS: 21
MILLISECONDS: 890
Total Hours: 2
Total Minutes: 136

Cookies are simple text files stored on the user's computer, and are used for securing this site.
OK