O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required