Add and Subtract Dates and Times

Problem

You need to perform calculations with dates and times.

Solution

Use the TimeSpan and DateTime structures, both of which provide Add and Subtract methods.

Discussion

The .NET Framework provides two structures for manipulating date and time information. The DateTime structure stores a reference to a single date and time (such as January 20, 2004 at 12:00 AM). The TimeSpan structure stores an interval of time (such as three hours). TimeSpan is ultimately measured in ticks (a unit of time equal to 100 nanoseconds), and DateTime is stored as the number of ticks since 12:00:00 midnight, January 1, 0001 C.E. The greater a DateTime is, the later the date. The greater a TimeSpan is, the larger the interval of time.

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.