Home

Time

 

Introduction

As defined earlier, the time is a value used to identify the number of units that have elapsed since a starting period, called midnight, of a day. A day is made of 24 non-spatial divisions; each one of these divisions is called an hour. An hour is made of 60 fractions and each one of these fractions is called a minute. A minute is divided in 60 parts, each called a second. As done with dates, most of the operations performed on time values are centered around the DateTime structure.

The rules used by the computer for time display can be seen in the Customize Regional Options dialog box. To access it, in the Control Panel, you can double-click Regional and Language Options. In the Regional Options tab of the Regional and Language Options dialog box, you can click Customize. In the Customize Regional Options, you can click Time:

Customize Regional Options

In US English, the symbol to separate the hour and the minute. An example would be: 08:25. In the same way, in US English, the character used to separate the minute and the second is :. An example would be: 08:25:44. In many cases, unless necessary, the second is not represented in a time value.

The time of a day can be considered as a value of 24 hours or the day can be considered as two fractions of 12 hours each. There is a period in the middle of day time called noon. It divides the 24-hour day in two parts, each consisting of 12 hours. The noon period is positioned 12 hours after midnight. The period that goes from midnight to the middle of day time is represented by the expression AM. The period from noon to midnight is represented by the PM expression. When a DateTime object includes an AM or PM but of unknown value, the section is represented as AM/PM and this expression is written on the right side of the seconds. To separate the seconds and the AM/PM, you (as well as the compiler) leave a one-character empty space after the seconds.

Creating a Time Value

The time portion of a DateTime structure can be declared and manipulated as a value. To create a time value, you can declare a DateTime variable using the following constructor:

public DateTime(int year, int month, int day,
		int hour, int minute, int second);

If you are more concerned about the time, the first, second, and third arguments are not important. You can pass each as 1. In this case, the date would be set as January 1, 0001. The other three arguments represent the components of a time value.

The minutes value must range from 0 to 59; otherwise, an error would be produced (when studying exception handling, we will see that this means that the compiler would throw an exception). The seconds value must be between 0 and 59 or the program would cause an error.

When initializing a date or time variable using this constructor, the hour value must be between 0 and 23. Any other value outside of this range will cause an error. If the hour portion has a value between 0 and 11, the time is set in the morning with the AM in the AM/PM section. Here is an example:

using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(1, 1, 1, 10, 24, 52);

            Console.WriteLine("Time: {0}\n", time);
            return 0;
        }
    }
}

This would produce:

Time: 1/1/0001 10:24:52 AM

Press any key to continue . . .

If the hour portion is between 12 and 23, the time is set in the afternoon. When displaying it, the compiler, by default, calculates and displays the 0 to 12 portion and then displays PM in the AM/PM section. Here is an example:

using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(1, 1, 1, 16, 8, 44);

            Console.WriteLine("Time: {0}\n", time);
            return 0;
        }
    }
}

This would produce:

Time: 1/1/0001 4:08:44 PM

Press any key to continue . . .

In extreme cases, you may want to use the milliseconds of a time value. When declaring and initializing the variable, you can use the following constructor:

public DateTime(int year, int month, int day,
		int hour, int minute, int second, int millisecond);

The milliseconds must range from 0 to 999. If you do not know the millisecond value, you can provide it as 0.

Retrieving a Time Value

In a certain application, you may want the user to supply a time value. As mentioned for the date, there are rules that you, the computer, and the user must follow to apply a valid formula. As mentioned earlier, the compiler refers to the computer to find out what a date looks like. A valid time can follow the format hh:nn AM/PM or hh:nn:ss or one of the valid combinations. When requesting a time from  the user, in case the user is not familiar with the rules (and you should assume that the user doesn't know them), specify what formula the user should follow. Here is an example:

using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime dateSubmitted;
            
            Console.Write("Enter a date (yyyy/mm/dd): ");
            dateSubmitted = DateTime.Parse(Console.ReadLine());

            Console.WriteLine("\nDate Submitted: {0}\n", dateSubmitted);
            return 0;
        }
    }
}

Here is an example of running the program: 

Enter a date (yyyy/mm/dd): 1996/08/16

Date Submitted: 8/16/1996 12:00:00 AM

Press any key to continue . . .
 

Previous Copyright © 2006 FunctionX, Inc. Next