The Characteristics of a Time Value


The Components of a Time

By now, we have seen that a time value is made of the hour, the minute, the second, and the millisecond parts. These are values you can specify when creating a time object using one of the appropriate constructors of the DateTime structure. If you request a time value from the user or if the application itself will provide it, you can retrieve its components.

To get the hour portion of an existing DateTime object, you can access its Hour property. To retrieve the minute side of a time value, access its Minute property. If you want to know the second value of a DateTime variable, you can call its Second property. In the same way, you can get the millisecond value of a time by accessing its Millisecond property.


The Time of Day of a DateTime Value

As seen so far, a DateTime variable always holds both a date and a time portions. In your program, you may want to get only the time of the variable. To support this, the DateTime structure is equipped with a property named TimeOfDay. This property produces the time value of an existing DateTime object. Here is an example of using it:

using System;

namespace DateAndTime
    class Program
        static int Main()
            DateTime time = new DateTime(2002, 4, 22, 16, 8, 44);

            Console.WriteLine("Date and Time: {0}\n", time);
            Console.WriteLine("Time of Day:   {0}\n", time.TimeOfDay);
            return 0;

This would produce:

Date and Time: 4/22/2002 4:08:44 PM

Time of Day:   16:08:44

Press any key to continue . . .

Previous Copyright © 2006 FunctionX, Inc. Next