Times Fundamentals

Introduction

A time is a value that identifies the number of units that have elapsed since a starting period. Within a, the starting period of time is called midnigh. 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.

The rules used by the computer for time display can be seen in the Customize Regional Options dialog box in the Time property page:

Customize Regional Options

In US English, the symbol to separate the hour and the minute is the colon ":". An example would be: 08:25. In the same way, in US English, the character used to separate the minute and the second is the colon ":". An example is: 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 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 the day is represented by the word AM. The period from noon to midnight is represented by the word PM. When a date includes an AM or PM word 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(s) a one-character empty space after the seconds.

Practical LearningPractical Learning: Introducing Time Values

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New -> Project...
  3. In the middle list, click Console App (.NET Framework)
  4. Set the name to GeorgetownDryCleaningServices5
  5. Click OK

Creating a Time Value

To support time values, the .NET Framework provides the DateTime structure.

Primary Characteristics of a Time Value

The Hour of a Time Value

A day is divided in 24 equal parts. Each part is named an hour. To support the hours of a day, the DateTime structure is equipped with a read-only integral property named Hour:

public int Hour { get; }

The hours are counted from 0, 1, 2, up to 23.

A Minute in an hour

An hour is divided in 60 equal values. Each value is named a minute. To support the minutes of an hour, the DateTime structure is equipped with a read-only integer property named Minute:

public int Minute { get; }

The minutes are counted from 0, 1, 2, up to 59.

A Second in a Minute

A minute is divided in 60 equal values. Each value is named a second. To support the seconds of a time value, the DateTime structure is equipped with a numeric property named Second:

public int Second { get; }

Milliseconds

A second is made of 1000 integral parts called millisecond each

public int Millisecond { get; }

The milliseconds are counted as 0, 1, 2, up to 999.

Time Creation

When declaring a variable, to let you provide the time value, the DateTime structure provides 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.

When initializing a 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);

            return 0;
        }
    }
}

To display a time value, you can pass the variable to the Console.Write() or the Console.WriteLine() method. 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.

Requesting a Time Value

To request a time from a user, first get a value produced by a Consle.ReadLine() call. Then convert that value. To let you perform this conversion, the DateTime structure is equipped with a method named Parse.

When requesting a time value from a user, there are rules that you, the computer, and the user must follow. The compiler refers to the computer to find out what a date looks like. A valid time can follow the formula 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.

Practical LearningPractical Learning: Formatting a Time Value

  1. In the Solution Explorer, right-click Program.cs -> Rename
  2. Type CleaningOrder to get CleaningOrder.cs, and press Enter
  3. Read the text on the message box and click Yes
  4. Change the document as follows:
    using System;
    
    namespace GeorgetownDryCleaningServices6
    {
        public class CleaningOrder
        {
            public static int Main(string[] args)
            {
                // Price of items
                const double PriceOneShirt = 1.25;
                const double PriceAPairOfPants = 2.15;
                const double PriceOtherItem = 3.45;
                const double TaxRate = 5.75;  // 5.75%
    
                // Basic information about an order
                string customerName, homePhone;
                DateTime orderDate = new DateTime();
                DateTime orderTime = new DateTime();
                // Unsigned numbers to represent cleaning items
                ushort numberOfShirts = 0, numberOfPants = 0, numberOfOtherItems = 0;
                // Each of these sub totals will be used for cleaning items
                double subTotalShirts, subTotalPants, subTotalOtherItems;
                // Values used to process an order
                double totalOrder, taxAmount, salesTotal;
                double amountTended = 0, moneyChange;
    
                Console.Title = "Georgetown Dry Cleaning Services";
    
                Console.WriteLine("===============================================");
                Console.WriteLine("-/-    Georgetown Dry Cleaning Services     -/-");
                Console.WriteLine("===============================================");
                // Request order information from the user
                Console.Write("Enter Customer Name:                ");
                customerName = Console.ReadLine();
                Console.Write("Enter Customer Phone:               ");
                homePhone = Console.ReadLine();
                Console.Write("Enter the order date (mm/dd/yyyy):  ");
                try
                {
                    orderDate = DateTime.Parse(Console.ReadLine());
                }
                catch (FormatException fexc)
                {
                    Console.BackgroundColor = ConsoleColor.DarkBlue;
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(fexc.Message);
                }
    
                Console.ResetColor();
    
                Console.Write("Enter the order time (HH:MM):  ");
                try
                {
                    orderTime = DateTime.Parse(Console.ReadLine());
                }
                catch (FormatException fexc)
                {
                    Console.BackgroundColor = ConsoleColor.DarkBlue;
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(fexc.Message);
                }
    
                Console.ResetColor();
    
                // Request the quantity of each category of items
                try
                {
                    Console.Write("Number of Shirts:                   ");
                    numberOfShirts = ushort.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("The value you typed for the number of " +
                              "shirts is not a valid number.");
                }
    
                try
                {
                    Console.Write("Number of Pants:                    ");
                    numberOfPants = ushort.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("The value you typed for the number of " +
                              "pants is not a valid number.");
                }
    
                try
                {
                    Console.Write("Number of Other Items:              ");
                    numberOfOtherItems = ushort.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("The value you typed for the number of " +
                              "other types of items is not a valid number.");
                }
    
                // Perform the necessary calculations
                subTotalShirts = numberOfShirts * PriceOneShirt;
                subTotalPants = numberOfPants * PriceAPairOfPants;
                subTotalOtherItems = numberOfOtherItems * PriceOtherItem;
                // Calculate the "temporary" total of the order
                totalOrder = subTotalShirts + subTotalPants + subTotalOtherItems;
    
                // Calculate the tax amount using a constant rate
                taxAmount = totalOrder * TaxRate / 100.00;
                // Add the tax amount to the total order
                salesTotal = totalOrder + taxAmount;
    
                Console.WriteLine("----------------------------------------------");
                // Communicate the total to the user...
                Console.WriteLine("The Total order is:                 {0:C}", salesTotal);
                // and request money for the order
                try
                {
                    Console.Write("Amount Tended?                      ");
                    amountTended = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You were asked to enter an amount of money but something bad happened.");
                }
    
                // Calculate the difference owed to the customer
                // or that the customer still owes to the store
                moneyChange = amountTended - salesTotal;
    
                Console.Clear();
                Console.Title = "Georgetown Dry Cleaning Services";
    
                // Display the receipt
                Console.WriteLine("=========================================");
                Console.WriteLine("-/- Georgetown Dry Cleaning Services  -/-");
                Console.WriteLine("=========================================");
                Console.WriteLine("Customer:   {0}", customerName);
                Console.WriteLine("Home Phone: {0}", homePhone);
                Console.WriteLine("Order Date: {0:D}", orderDate);
                Console.WriteLine("Order Time: {0}", orderTime);
                Console.WriteLine("-----------------------------------------");
                Console.WriteLine("Item Type    Qty Unit/Price Sub-Total");
                Console.WriteLine("-----------------------------------------");
                Console.WriteLine("Shirts{0,9}     {1:C}     {2:C}",
                          numberOfShirts, PriceOneShirt, subTotalShirts);
                Console.WriteLine("Pants{0,10}     {1:C}     {2:C}",
                          numberOfPants, PriceAPairOfPants, subTotalPants);
                Console.WriteLine("Other Items{0,4}     {1:C}     {2:C}",
                          numberOfOtherItems, PriceOtherItem, subTotalOtherItems);
                Console.WriteLine("-----------------------------------------");
                Console.WriteLine("Total Order:     {0:C}", totalOrder);
                Console.WriteLine("Tax Rate:        {0:P}", TaxRate / 100);
                Console.WriteLine("Tax Amount:      {0:C}", taxAmount);
                Console.WriteLine("Net Price:       {0:C}", salesTotal);
                Console.WriteLine("-----------------------------------------");
                Console.WriteLine("Amount Tended:   {0:C}", amountTended);
                Console.WriteLine("Difference:      {0:C}", moneyChange);
                Console.WriteLine("=========================================");
    
                return 0;
            }
        }
    }
  5. To execute the program, on the main menu, click Debutg -> Start Without Debugging:
    ===============================================
    -/-    Georgetown Dry Cleaning Services     -/-
    ===============================================
    Enter Customer Name:
  6. Type some values as follows:
    ===============================================
    -/-    Georgetown Dry Cleaning Services     -/-
    ===============================================
    Enter Customer Name:                Borris Yulyn
    Enter Customer Phone:               (164) 317-8170
    Enter the order date (mm/dd/yyyy):  05/18/2019
    Enter the order time (HH:MM):       42:97
    The DateTime represented by the string is not supported in calendar System.Globa
    lization.GregorianCalendar.
    Number of Shirts:                   8
    Number of Pants:                    2
    Number of Other Items:              5
    ----------------------------------------------
    The Total order is:                 $33.36
    Amount Tended?                      40
  7. Press Enter:
    =========================================
    -/- Georgetown Dry Cleaning Services -/-
    =========================================
    Customer:   Borris Yulyn
    Home Phone: (164) 317-8170
    Order Date: Saturday, May 18, 2019
    Order Time: 1/1/0001 12:00:00 AM
    -----------------------------------------
    Item Type    Qty Unit/Price Sub-Total
    -----------------------------------------
    Shirts        8     $1.25     $10.00
    Pants         2     $2.15     $4.30
    Other Items   5     $3.45     $17.25
    -----------------------------------------
    Total Order:     $31.55
    Tax Rate:        5.75 %
    Tax Amount:      $1.81
    Net Price:       $33.36
    -----------------------------------------
    Amount Tended:   $40.00
    Difference:      $6.64
    =========================================
    Press any key to continue . . .
  8. Press Enter to close the window and return to your programming environment

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 . . .

Operations on Time Values

Converting a Time Value to a String

If you have a DateTime object, you can convert it to a String value. To make this operation possible, the DateTime structure is equipped with a method named ToString. The default version of this method takes no argument and it simply creates a string out of a time value. Here is an example:

using System;

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

            Console.WriteLine("Date and Time(DateTime): {0}\n", time);
            Console.WriteLine("Date and Time(String):   {0}\n", strTime);
            return 0;
        }
    }
}

This would produce:

Date and Time(DateTime): 4/22/2002 4:08:44 PM

Date and Time(String):   4/22/2002 4:08:44 PM

Press any key to continue . . .

Notice that the value produced by the string includes both the date, the time and even the AM/PM section. In some cases, you would be interested only in either the date or the time. To support this type of operation, the DateTime structure has another overloaded version of the ToString() method that takes as argument a String value. Its syntax is:

public string ToString(string format);

When calling this method, there are rules you should/must follow.

Time Rules and Formats

Like dates, time values follow the Regional (and Language) Settings of Control Panel when they display. To make this display friendlier, Microsoft Windows provides the rules, through some characters, you can use to format a time:

Time

 The characters used to create a format are:

Format Used For Description
hh Hours An hour number from 0 to 23.
If the hour is less than 10, it would display with the leading 0 such as 08. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(2002, 4, 22, 5, 8, 37);
            string strHour = time.ToString("hh");

            Console.WriteLine("Date and Time: {0}", time);
            Console.WriteLine("Hour Value:    {0}\n", strHour);
            return 0;
        }
    }
}
This would produce:
Date and Time: 4/22/2002 5:08:37 AM
Hour Value:    05

Press any key to continue . . .

If the hour is greater than 12, such as 15, 12 would be subtract from it and it would display with a leading 0, such as 05. Here is an example:

using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
            string strHour = time.ToString("hh");

            Console.WriteLine("Date and Time: {0}", time);
            Console.WriteLine("Hour Value:    {0}\n", strHour);
            return 0;
        }
    }
}
This would produce:
Date and Time: 4/22/2002 5:08:37 AM
Hour Value:    05

Press any key to continue . . .
HH Hours An hour number from 0 to 23.
If the hour is less than 10, it would display with the leading 0 such as 08. The effect is the same as seen for the hh format.
If the hour is greater than 12, such as 15, it would display with that value. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
            string strHour = time.ToString("HH");

            Console.WriteLine("Date and Time: {0}", time);
            Console.WriteLine("Hour Value:    {0}\n", strHour);
            return 0;
        }
    }
}
This would produce:
Date and Time: 4/22/2002 3:08:37 PM
Hour Value:    15

Press any key to continue . . .
mm Minutes A minute number from 0 to 59.
If the number is less than 10, it would display with the leading 0 such as 06. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
            string strMinute = time.ToString("mm");

            Console.WriteLine("Date and Time: {0}", time);
            Console.WriteLine("Minute Value:    {0}\n", strMinute);
            return 0;
        }
    }
}

This would produce:

Date and Time: 4/22/2002 3:08:37 PM
Minute Value:    08

Press any key to continue . . .

If the number is greater than 9, it would display the same.

ss Seconds A second value from 0 to 59.
If the number is less than 10, it would display with the leading 0 such as 04. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(2002, 4, 22, 15, 8, 7);
            string strSecond = time.ToString("ss");

            Console.WriteLine("Date and Time: {0}", time);
            Console.WriteLine("Hour Value:    {0}\n", strSecond);
            return 0;
        }
    }
}
This would produce:
Date and Time: 4/22/2002 3:08:07 PM
Hour Value:    07

Press any key to continue . . .

If the number is greater than 9, it would display like that. 

tt
or
tttt
AM/PM Expression used to produce the AM or PM side of a time value. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(2002, 4, 22, 5, 8, 7);
            string strAMPM = time.ToString("tt");

            Console.WriteLine("Date and Time: {0}", time);
            Console.WriteLine("AM/PM Side:    {0}\n", strAMPM);
            return 0;
        }
    }
}

This would produce:

Date and Time: 4/22/2002 5:08:07 AM
AM/PM Side:    AM

Press any key to continue . . .

To create or format a (complete) time value, you must use a combination of characters:

Format Used For Description
: Separator The character separator for time values.
This character is set in the Regional (and Language) Settings of Control Panel.
Don't use this character by itself in a ToString() method.
h or H Hours An hour number from 0 to 23.
If the hour is less than 10, it would display without the leading 0.
Don't use the letter "h" or "H" by itself in a ToString() method
m Minutes A minute number from 0 to 59.
If the number is less than 10, it would display without the leading 0. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
            string strMinute = time.ToString("h:m");

            Console.WriteLine("Date and Time: {0}", time);
            Console.WriteLine("Minute Value:    {0}\n", strMinute);
            return 0;
        }
    }
}

This would produce:

Date and Time: 4/22/2002 3:08:37 PM
Minute Value:    3:8

Press any key to continue . . .

If the minute number is greater than 9, it would display as such.
Don't use the letter "m" by itself in a ToString() method.

mm Minutes A minute number from 0 to 59.
If the number is less than 10, it would display with a leading 0. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
            string strMinute = time.ToString("h:mm");

            Console.WriteLine("Date and Time: {0}", time);
            Console.WriteLine("Minute Value:    {0}\n", strMinute);
            return 0;
        }
    }
}

This would produce:

Date and Time: 4/22/2002 3:08:37 PM
Minute Value:    3:08

Press any key to continue . . .

Don't pass "mm" by itself in a ToString() method

s Seconds A second value from 0 to 59.
If the number is less than 10, it would display without a leading 0. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(2002, 4, 22, 15, 8, 7);
            string strSecond = time.ToString("h:mm:s");

            Console.WriteLine("Date and Time: {0}", time);
            Console.WriteLine("Second Value:    {0}\n", strSecond);
            return 0;
        }
    }
}

This would produce:

Date and Time: 4/22/2002 3:08:07 PM
Minute Value:    3:08:7

Press any key to continue . . .

Don't pass "mm" by itself in a ToString() method

ss Seconds A second value from 0 to 59.
If the number is less than 10, it would display with a leading 0. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime time = new DateTime(2002, 4, 22, 15, 8, 7);
            string strSecond = time.ToString("h:mm:ss");

            Console.WriteLine("Date and Time: {0}", time);
            Console.WriteLine("Second Value:    {0}\n", strSecond);
            return 0;
        }
    }
}

This would produce:

Date and Time: 4/22/2002 3:08:07 PM
Minute Value:    3:08:07

Press any key to continue . . .

Don't pass "mm" by itself in a ToString() method

A Short Time Value

A short time displays its value as HH:MM Am/PM. To let you convert a time value to this format, the DateTime structure is equipped with a method named ToShortTimeString. Its syntax is:

public string ToShortTimeString();

Practical LearningPractical Learning: Formatting a Time Value

  1. Change the CleaningOrder.cs document as follows:
    using System;
    
    namespace GeorgetownDryCleaningServices6
    {
        public class CleaningOrder
        {
            public static int Main(string[] args)
            {
                . . . No Change
    
    
                // Display the receipt
                Console.WriteLine("=========================================");
                Console.WriteLine("-/- Georgetown Dry Cleaning Services  -/-");
                Console.WriteLine("=========================================");
                Console.WriteLine("Customer:   {0}", customerName);
                Console.WriteLine("Home Phone: {0}", homePhone);
                Console.WriteLine("Order Date: {0:D}", orderDate);
                Console.WriteLine("Order Time: {0}", orderTime.ToShortTimeString());
                Console.WriteLine("-----------------------------------------");
                Console.WriteLine("Item Type    Qty Unit/Price Sub-Total");
                Console.WriteLine("-----------------------------------------");
                Console.WriteLine("Shirts{0,9}     {1:C}     {2:C}",
                          numberOfShirts, PriceOneShirt, subTotalShirts);
                Console.WriteLine("Pants{0,10}     {1:C}     {2:C}",
                          numberOfPants, PriceAPairOfPants, subTotalPants);
                Console.WriteLine("Other Items{0,4}     {1:C}     {2:C}",
                          numberOfOtherItems, PriceOtherItem, subTotalOtherItems);
                Console.WriteLine("-----------------------------------------");
                Console.WriteLine("Total Order:     {0:C}", totalOrder);
                Console.WriteLine("Tax Rate:        {0:P}", TaxRate / 100);
                Console.WriteLine("Tax Amount:      {0:C}", taxAmount);
                Console.WriteLine("Net Price:       {0:C}", salesTotal);
                Console.WriteLine("-----------------------------------------");
                Console.WriteLine("Amount Tended:   {0:C}", amountTended);
                Console.WriteLine("Difference:      {0:C}", moneyChange);
                Console.WriteLine("=========================================");
    
                return 0;
            }
        }
    }
  2. To execute the program, on the main menu, click Debutg -> Start Without Debugging:
    ===============================================
    -/-    Georgetown Dry Cleaning Services     -/-
    ===============================================
    Enter Customer Name:
  3. Type some values as follows:
    ===============================================
    -/-    Georgetown Dry Cleaning Services     -/-
    ===============================================
    Enter Customer Name:                Borris Yulyn
    Enter Customer Phone:               (164) 317-8170
    Enter the order date (mm/dd/yyyy):  05/18/2019
    Enter the order time (HH:MM):       12:57
    Number of Shirts:                   8
    Number of Pants:                    2
    Number of Other Items:              5
    ----------------------------------------------
    The Total order is:                 $33.36
    Amount Tended?                      40
  4. Press Enter:
    =========================================
    -/- Georgetown Dry Cleaning Services -/-
    =========================================
    Customer:   Borris Yulyn
    Home Phone: (164) 317-8170
    Order Date: Saturday, May 18, 2019
    Order Time: 12:57 PM
    -----------------------------------------
    Item Type    Qty Unit/Price Sub-Total
    -----------------------------------------
    Shirts        8     $1.25     $10.00
    Pants         2     $2.15     $4.30
    Other Items   5     $3.45     $17.25
    -----------------------------------------
    Total Order:     $31.55
    Tax Rate:        5.75 %
    Tax Amount:      $1.81
    Net Price:       $33.36
    -----------------------------------------
    Amount Tended:   $40.00
    Difference:      $6.64
    =========================================
    Press any key to continue . . .
  5. Press Enter to close the window and return to your programming environment

A Long TimeValue

A long time displays its value as HH:MM:SS AM/PM. To support this, the DateTime structure is equipped with a method named ToLongTimeString. Its syntax is:

public string ToLongTimeString();

Comparison Operations On Time Values

As done for date values, you can compare time values to find out if one occurs before another or whether they occur at the same time. To support the comparisons, the DateTime structure is equipped with the Compare() method.

Combining Date and Time

The Current Date and Time

When a user starts a computer and while using it, it keeps a date and time values referred to as local date and time or system date and time. Depending on your application, at one time you may need to get one or both of these pieces of information. It is important to know that the computer, not you, controls this information (but you can programmatically change it if you want). To support both, the DateTime structure is equipped with a static property named Now. This property holds the year, the month, the day, the name of the day, the hour, the minute, and the second. Here is an example of calling it:

using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            Console.WriteLine("System Date and Time: {0}", DateTime.Now);
            return 0;
        }
    }
}

Here is an example of running the program:

System Date and Time: 10/15/2006 3:08:48 PM
Press any key to continue . . .

To get the current date of the computer, the DateTime structure provides a static property named Today. Here is an example:

using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            Console.WriteLine("Current Date: {0}", DateTime.Today);
            return 0;
        }
    }
}

Here is an example of running the program:

Current Date: 10/15/2006 12:00:00 AM
Press any key to continue . . .

Formatting Date and Time Combinations

Although the DateTime structure is equipped to produce default values of a date and time in combination, you can use the formats we have reviewed to create your own date, in the sequences of your choice, such as a time preceding a date, a value providing only the month and the minutes, etc.


Previous Copyright © 2008-2019, FunctionX Next