PROWARE technologies
PROWARE technologies

Working with Dates in .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: 12/16/2019 7:34:29 PM minus 12/14/2019 11:40:49 PM
DAYS: 1
HOURS: 19
MINUTES: 53
SECONDS: 40
MILLISECONDS: 65
Total Hours: 43
Total Minutes: 2633

Contact