5.3. DateTime Reference Objects

Many business applications need to manipulate dates and times. Banks need to calculate interest payments on loans based on dates. Producing certain products, like bread, requires that specific ingredients be added after so many minutes or hours have passed. Visual Studio provides a DateTime data type to enable you to work with dates and time easily.

The way Visual Studio tracks time is pretty interesting. The Windows operating system maintains a system clock as part of its duties. Most of you can observe the current local time in the lower right-hand corner of your display. Visual Studio uses the system clock to track the number of ticks on the system clock. A tick is a unit of time measured in 100-nanosecond blocks. A nanosecond is one billionth of a second. That's a fairly small time slice. In fact, light only travels about 18 inches in a nanosecond.

The DateTime object maintains a count of the number of ticks since midnight, January 1, 0001 A.D., using the Gregorian calendar. That's a whole bunch of ticks to the present! However, by manipulating the tick count, the DataTime object is able to determine all kinds of things relative to times and dates.

Figure 5-10 shows a sample run of a program that uses some of the DateTime methods and properties.

Figure 5-10. Figure 5-10

5.3.1. DateTime and ToString() Conversions

As I mentioned earlier, all ...

Get Beginning C# 3.0 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.