Time Fundamentals

Introduction

A time is a value to identify the number of units that have elapsed since midnight of a day.

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

Customize Regional Options

Creating a Time Value

To create a time value, use the DateTime structure we reviewed for date values. Therefore, you can declare a variable of that type. The primary components of a time value are named hour, minute, and second. An hour is one of 24 parts of a day. A minute is one of 60 parts of an hour. A minute is divided in 60 parts named second each. Therefore, to start a time value, 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 must range from 0 to 59; otherwise, an exception would be thrown. The seconds 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(1, 1, 1, 10, 24, 52);

        WriteLine("Time: {0}", time);
        WriteLine("=================================");
        return 10;
    }
}

Primary Characteristics of a Time Value

Introduction

The pieces of information of the DateTime structure are:

The Time Separator

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

Therefore, the above program will produce:

Time: 1/1/0001 10:24:52 AM
=================================
Press any key to continue . . .

The AM/PM Factor

A day is considered in two parts referred to as morning or before noon and afternoon. You can keep this in mind when specify the parts of a time value. For example, when declaring a DateTime variable, if the hour portion is between 12 and 23, the time is set in the afternoon. Here is an example:

using System;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        DateTime time = new DateTime(1, 1, 1, 16, 8, 44);

        WriteLine("Time: {0}", time);

This would produce:

Time: 1/1/0001 4:08:44 PM
=================================
Press any key to continue . . .

The Milliseconds of a Time Value

In some 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 don't know the millisecond value, you can provide it as 0.

Retrieving a Time Value

In a certain application, you may want the visitor to provide a time value. 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.

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 design, 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 16, 8, 44);

        WriteLine("Date and Time: {0}", time);
        WriteLine($"Time of Day:   {time.TimeOfDay}");
        WriteLine("===================================");
        return 10;
    }
}

This would produce:

Date and Time: 4/22/2002 4:08:44 PM
Time of Day:   16:08:44
===================================
Press any key to continue . . .

Windows Controls: The Masked Text Box

Description

The regular text box in Microsoft Windows allows the user to enter any type of string in the control. In some cases, you may want to exercise more control on what the user can enter in a text box. For example, if you provide a text box for a date, the user can still enter a person's name. If you create a text box for a telephone number, the user may instead enter somebody's salary. To assist the user with entering a specific value into a text box, the .NET Framework provides the masked text box.

The masked text box allows you to configure one or more placeholders in the field of the control so that the control can accept some characters, must reject some characters, and/or can display some other characters you will have put so the user cannot delete them.

Creating a Masked Text Box

To support masked text boxes, the .NET Framework provides a class named MaskedTextBox. To visually create a masked text box, from the Common Controls section of the Toolbox, click MaskedTextBox MaskedTextBox and click the form. To programmatically create a masked text box, declare a variable of type MaskedTextBox and use the New operator to initialize it. Here is an example:

using System.Windows.Forms;

public class Exercise : Form
{
    MaskedTextBox txtFunctional;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        txtFunctional = new MaskedTextBox();
    }
}

The masked text box uses the same common characteristics of other visual control: location, size, etc.

Introduction to the Characteristics of the Masked Text Box

The masked text box uses the same common characteristics of other visual controls: name, location, size, background color, border size, anchoring, docking, font, etc. Like the regular text box, the MaskedTextBox class is derived from TextBoxBase that provides its fundamental properties.

The Mask

The primary reason for using a masked text box is to control short lengths of text entered into it. Probably the most important property of a masked text box, which sets it apart from the (traditional) text box control, is its ability to control what the user can and cannot enter in the text side. To configure this text, the MaskedTextBox class is equipped with a property named Mask:

public string Mask { get; set; }

There are two main ways you can configure it:

Any of these actions would open the Input Mask dialog box:

Input Mask

The Input Mask dialog box provides some of the most regularly used masks in Windows and database applications (the contents of the Input Mask dialog box depends on the language settings of the computer on which you are using Microsoft Visual Studio; for example, the above Input Box is adapted for US English with the US Social Security Number and telephone format). To use an existing mask, you can click it and click OK.

To programmatically specify a mask, create it in a string and assign that string to the Mask property. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        MaskedTextBox txtFunctional = new MaskedTextBox();
        txtFunctional.Location = new Point(12, 12);
        
        txtFunctional.Mask = "999-000-0000";

        Text = "Mask Text Box";
    }
}

This would produce:

Mask

Introductory Characteristics of a Time Value

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, as mentioned previously, tthe DateTime structure overrides the ToString() method. 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 16, 8, 44);
        string strTime = time.ToString();

        WriteLine("Date and Time (DateTime): {0}", time);
        WriteLine("Date and Time (String):   {0}", strTime);
        WriteLine("===============================================");
        return 10;
    }
}

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.

Introduction to Time Rules and Formats

Like dates, time values follow some rules:

Time

Introduction to Formatting a Time Value

As mentioned already, a time value must use some pre-determined rules to be appropriately readable and interpreted. The rules include the use of some specific letters and symbols. The formats we will review can be used on a text-based value (console window, text box control, label, list-based controls, etc) presented to the user or on a Windows control (time picker, etc).

In our review, we will indicate some letters in uppercase or in lowercase. The format are somehow case-sensitive. This means that if you are are to use something like "hh" or "HH", don't use "Hh". Also, don't a a character inside of an indicated combination. This means that "hh" is not the same as "h h" or "h-h".

Hour Formats

Introduction

The h and the H are used for the hour values. Don't use the letter "h" or "H" by itself in a ToString() method. If you do, you will receive a FormatException exception.

hh

The "hh" combination is used for an hour number from 0 to 12 and the compiler must know if the time is happening in the morning or afternoon (including evening). If the hour is from midnight to noon, which is from 0 to 12, if the less than 10, it would display with the leading 0. Here is an example:

using System;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 5, 8, 37);
        string strHour = time.ToString("hh");

        WriteLine("Date and Time: {0}", time);
        WriteLine("Hour Value:    {0}", strHour);
        WriteLine("=====================================");
        return 10;
    }
}

This would produce:

Date and Time: 4/22/2002 5:08:37 AM
Hour Value:    05
=====================================
Press any key to continue . . .

If the hour occurs in the afternoon, im which case the hour value 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
        string strHour = time.ToString("hh");

        WriteLine("Date and Time: {0}", time);
        WriteLine("Hour Value:    {0}", strHour);
        WriteLine("=====================================");
        return 20;
    }
}

This would produce:

Date and Time: 4/22/2002 3:08:37 PM
Hour Value:    03
=====================================
Press any key to continue . . .

HH

The HH expression is used for an hour number from 0 to 23. If the hour is less than 10, it would display with the leading 0. 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
        string strHour = time.ToString("HH");

        WriteLine("Date and Time: {0}", time);
        WriteLine("Hour Value:    {0}", strHour);
        WriteLine("=====================================");
        return 30;
    }
}

This would produce:

Date and Time: 4/22/2002 3:08:37 PM
Hour Value:    15
=====================================
Press any key to continue . . .

Minute Formats

mm

The mm combination is used for minutes. A minute is a 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
        string strMinute = time.ToString("mm");

        WriteLine("Date and Time: {0}", time);
        WriteLine("Minute Value:  {0}", strMinute);
        WriteLine("=====================================");
        return 40;
    }
}

This would display:

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.

The Time Separator :

To express a time value, you use some letters or a combination of some letters and some symbols. If you use a combination of letters and symbols, you must use some specific symbols. For example, the letters must be separated by some specific symbols. The character used to separate the parts of a date is set in the Regional (and Language) Settings of Control Panel. In US English, this character is the colon (:). Don't pass that character by itself to the ToString() method.

m

The letter m is used to format the minutes. This letter can be combined to the h letters used for the hour. In this case, the letter m would follow the letter h after a time separator, as in h:m.

A minute is a 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
        string strMinute = time.ToString("h:m");

        WriteLine("Date and Time: {0}", time);
        WriteLine("Minute Value:  {0}", strMinute);
        WriteLine("=====================================");
        return 50;
    }
}

This would display:

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.

You can also use variants of the h and m letters to create an expression that will display the time in a format of your choice. Here are examples:

using System;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 5, 8, 37);
        string strMinute1 = time.ToString("h:m");
        string strMinute2 = time.ToString("hh:m");
        string strMinute3 = time.ToString("h:mm");
        string strMinute4 = time.ToString("hh:mm");

        WriteLine("Date and Time: {0}", time);
        WriteLine("Time Value:  {0}", strMinute1);
        WriteLine(string.Format("Time Value:  {0}", strMinute2));
        WriteLine($"Time Value:  {strMinute3}");
        WriteLine($"Time Value:  {strMinute4}");
        WriteLine("=====================================");
        return 50;
    }
}

This would display:

Date and Time: 4/22/2002 5:08:37 AM
Time Value:  5:8
Time Value:  05:8
Time Value:  5:08
Time Value:  05:08
=====================================
Press any key to continue . . .

Seconds Formats

ss

The ss combination is used for 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 15, 8, 7);
        string strSecond = time.ToString("ss");

        WriteLine("Date and Time: {0}", time);
        WriteLine("Hour Value:    {0}", strSecond);
        WriteLine("=====================================");
        return 10;
    }
}

This would display:

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.

Involving Seconds in a Time Value

One way to display a complete time time is to add the parts for the seconds to a combination of the hour(s) and minute(s). This is done by following the minutes part with a time separator and the s format.

The s letter is used to show the seconds of a time value. A second is a 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 15, 8, 7);
        string strSecond = time.ToString("h:mm:s");

        WriteLine("Date and Time: {0}", time);
        WriteLine("Second Value:  {0}", strSecond);
        WriteLine("=====================================");
        return 10;
    }
}

This would display:

Date and Time: 4/22/2002 3:08:07 PM
Second Value:  3:08:7
=====================================
Press any key to continue . . .

The ss combination is used for seconds. A second is a 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 15, 8, 7);
        string strSecond = time.ToString("h:mm:ss");

        WriteLine("Date and Time: {0}", time);
        WriteLine("Second Value:  {0}", strSecond);
        WriteLine("=====================================");
        return 10;
    }
}

This would display:

Date and Time: 4/22/2002 3:08:07 PM
Second Value:  3:08:07
=====================================
Press any key to continue . . .

tt or tttt

The tt and the tttt combinations are used for the AM/PM period. This means that the tt and tttt expressions are used to produce the AM or PM side of a time value. Here is an example:

using System;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Time Values";

        DateTime time = new DateTime(2002, 4, 22, 5, 8, 7);
        string strAMPM = time.ToString("tt");

        WriteLine("Date and Time: {0}", time);
        WriteLine("AM/PM Side:    {0}", strAMPM);
        WriteLine("=====================================");
        return 10;
    }
}

This would display:

Date and Time: 4/22/2002 5:08:07 AM
AM/PM Side:    AM
=====================================
Press any key to continue . . .

Windows Controls: The Time Picker

Introduction

The time picker is a control that allows the user to select a time value:

 Date/Time Picker

One of the advantages of the time picker control is that it allows the user to select a time value instead of typing it. This tremendously reduces the likelihood of mistakes.

Creating a Time Picker

To support the time picker control, the .NET Framework provides the DateTimePicker class that we reviewed already. To visually add a time picker to an application, in the Common Controls section of the Toolbox, click the DateTimePicker control DateTimePicker and click a form or other container. To transform a date time picker into a time picker control, change its Format property to a Time value. This can be done programmatically as follows:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        DateTimePicker dtpTimeArrived = new DateTimePicker();
        dtpTimeArrived.Location = new Point(12, 12);
        dtpTimeArrived.Format = DateTimePickerFormat.Time;

    }
}

Although optional, you should (with emphasis) also set the ShowUpDown Boolean property to True:

void InitializeComponent()
{
    dtpTimeArrived = new DateTimePicker();
    dtpTimeArrived.Location = new Point(12, 12);
    dtpTimeArrived.ShowUpDown = true;
    dtpTimeArrived.Format = DateTimePickerFormat.Time;
}

This makes it a true time picker control:

Time Picker

If you do not set the ShowUpDown property to true, the control would display as a combo box and when the user clicks the arrow button, a calendar would come, which does not make sense since the purpose of the control is to deal with time values.

Using the Time Picker

The time picker is meant either to only display a time to the user or to both display time and allow the user to specify a time. The control follows the format of the time values set in the regional settings of Control Panel. By default, in US English, the time is divided in four sections that include the hour value, the minute value, the second value, and the AM/PM side. These sections are separated by standard characters that also are specified in the regional settings of Control Panel.

To change the time, the user can click a section, such as the hour, the minute, the second, or the AP/PM side, then click one of the arrows of the spin button. The up button increases (only) the value of the selected section while the down button decreases (only) the value of the selected section. The user can also use the arrow keys to change the value of a section.

After changing the value of the time, even the change occurs for only one section, the control fires a ValueChanged event, which is the default event of the control. Here is an example of using it:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
    DateTimePicker dtpTimeArrived;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        dtpTimeArrived = new DateTimePicker();
        dtpTimeArrived.Location = new Point(12, 12);
        dtpTimeArrived.ShowUpDown = true;
        dtpTimeArrived.Format = DateTimePickerFormat.Time;
        dtpTimeArrived.ValueChanged += new EventHandler(dtpTimeArrivedValueChanged);
    }

    void dtpTimeArrivedValueChanged(object sender, EventArgs e)
    {

    }
}

The Value of the Time Picker

As mentioned already, the user changes the time by using the various sections of the control. If the user changes the value of a section, it is considered that the whole time has been changed. At any time, the (current) time of the control is stored in a property named Value. You can use this property either to programmatically change the time on the control or to get the time on the control.

Using a Custom Format

By default, the time displays using the H:M:SS AM/PM format. This means that the time uses 1 digit for the hours from 0 to 9, 1 digit for the minutes from 0 to 9, 1 digit for the seconds from 0 to 9 and the AM or PM for morning or afternoon. To customize the way the time displays, first set the Format property to Custom. Then, in the CustomFormat property, use a combination of the following characters to create a custom format:

Format Used For Description
h Hour for 12-hour basis Used to display the hour with one digit if the value is less than 10
hh Hour for 12-hour basis Used to display the hour with a leading 0 if the value is less than 10
H Hour for 24-hour basis Used to display the hour with one digit if the value is less than 10
HH Hour for 24-hour basis Used to display the hour with a leading 0 if the value is less than 10
m Minute Used to display the minute with one digit if the value is less than 10
mm Minute Used to display the minute with a leading 0 if the value is less than 10
t AM/PM Displays the letter A or P for the AM or PM section
tt AM/PM Displays the letters AM or PM for the last section

You can set the format at design time using the Format field on the Properties window. To set the format at run time, assign the desired format to the DateTimePicker.CustomFormat property.

By default, after adding the control to the form or container, it assumes the time of the computer when the control was added. If you want to set a different time, apply a Format combination to the Value property. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
    DateTimePicker dtpTimeArrived;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        dtpTimeArrived = new DateTimePicker();
        dtpTimeArrived.Location = new Point(40, 12);
        dtpTimeArrived.ShowUpDown = true;
        dtpTimeArrived.Format = DateTimePickerFormat.Custom;
        dtpTimeArrived.CustomFormat = "hh:mm tt";

        Controls.Add(dtpTimeArrived);
        Text = "Time Picker Example";
    }
}

public class Program
{
    static int Main(string[] args)
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}
Time Picker

In the same way, at any time, you can retrieve the time value on the control by accessing the Value property.

Windows Controls: A Timer

Introduction

A timer is a non-spatial object that uses recurring lapses of time in a computer or in your application. To work, every lapse of period, the control sends a message to the operating system. The message is something to the effect of "I have counted the number of lapses you asked me to count".

As opposed to the time that controls your computer, a timer is partly but greatly under your control. Users do not see nor do they use a timer as a control. As a programmer, you decide if, why, when, and how to use this control.

To support timers, the .NET Framework provides a class named Timer. You can use that class to programmatically create a timer. To let you visually add a timer to your application, the Toolbox is equipped with a button named Timer. It is available in the Components section of the Toolbox. You can click that control and click your form. The timer is not a visual control. Therefore, if you visually add it to your form, it would only be represented by a button below the form.

Characteristics of a Timer

A timer is an object used to count lapses of time and send a message when it has finished counting. Each count is called a tick. When a tick occurs, the control fires an event named Tick. This Tick event is of type EventArgs, meaning that it doesn't provide more information than to let you know that a lapse has occurred. Here is an example of implementing that event:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    Timer tmrFlash;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        tmrFlash = new  Timer ();
        tmrFlash.Tick += new EventHandler(btnFlashTick);

        Text = "Text Flasher";
        Size = new System.Drawing.Size(435, 88);
    }

    void btnFlashTick(object sender, EventArgs e)
    {
    }
}

public class Program
{
    static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

The amount of time allocated for counting is called an interval and it is represented by the Interval property. The Interval property is a very important characteristic of the Timer control because it measures and controls the total time needed to perform a complete count. The Interval is measured in milliseconds. Like any counter, the lower the value, the faster the count, and the higher the value, the longer the count. The amount of interval you specify will depend on what you are trying to do. Here is an example of specifying the interval:

void InitializeComponent()
{
    tmrFlash = new  Timer ();
    tmrFlash.Interval = 500;
    tmrFlash.Tick += new EventHandler(btnFlashTick);
}

In order for a timer to count, you must tell it when it should start counting. In some applications, you may want the control to work full-time while in some other applications, you may want the control to work only in response to an intermediate event. The ability to stop and start a Timer control can be set using a Boolean property named Enabled. Here is an example of enabling a timer:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    Timer tmrFlash;
    Label lblFlash;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        tmrFlash = new  Timer();
        tmrFlash.Interval = 500;
        tmrFlash.Enabled = true;
        tmrFlash.Tick += new EventHandler(btnFlashTick);

        lblFlash = new Label();
        lblFlash.AutoSize = true;
        lblFlash.Text = "C# Programming is Fun!!!";
        lblFlash.Font = new System.Drawing.Font("Square721 BT", 24F);
        lblFlash.ForeColor = System.Drawing.Color.Blue;
        lblFlash.Location = new System.Drawing.Point(12, 10);

        Text = "Text Flasher";
        Size = new System.Drawing.Size(435, 88);
        Controls.Add(lblFlash);
    }

    void btnFlashTick(object sender, EventArgs e)
    {
        if (lblFlash.Text == "C# Programming is Fun!!!")
            lblFlash.Text = "";
        else
            lblFlash.Text = "C# Programming is Fun!!!";
    }
}

public class Program
{
    static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

When, or as soon as, this property is set to true, the control starts counting. You can also make it start by calling the Timer.Start() method. Its syntax is:

public void Start();

If, when, or as soon as, the Enabled property is set to false, the control stops and resets its counter to 0. You can also stop the timer by calling the Timer.Stop() method. Its syntax is:

public void Stop();

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 accessing it:

using System;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Date and Time Values";

        WriteLine(string.Format("System Date and Time: {0}", DateTime.Now));
        WriteLine("============================================");
        return 10;
    }
}

Here is an example of running the program:

System Date and Time: 9/10/2020 10:18:41 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;
using static System.Console;

public class Exercise
{
    public static int Main(string[] args)
    {
        Title = "Date and Time Values";

        WriteLine(string.Format("Current Date: {0}", DateTime.Today));
        WriteLine("====================================");
        return 10;
    }
}

Here is an example of running the program:

Current Date: 9/10/2020 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 © 2001-2021, FunctionX Next