Home

Dates

 

Dates

 

Date Creation

A date is a technique of identifying a period using three numbers: the year, the month, and the day. Each is an integer value. If you know the values you want to use to create or specify a date value, when declaring a DateTime variable, you can use the following constructor:

public DateTime(int year, int month, int second)

Here is an example of creating a date:

using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime ind = new DateTime(1960, 1, 1);

            Console.WriteLine("Independence Day: {0}", ind);
            return 0;
        }
    }
}

This would produce:

Independence Day: 1/1/1960 12:00:00 AM
Press any key to continue . . .

Converting a String to Date

Before displaying a DateTime value in your application, you can first convert it to a string. To support this, the DateTime structure provides the ToString() method that is overloaded with various versions. One of the versions takes no argument and its syntax is:

public override string ToString();

If you call this version of the method, the compiler uses a default format depending on the language set on the user's computer. If you want to control how the date should be rendered, you can use the version of the ToString() method that takes as argument a String value.

Rules of Date Formats

The computer uses two main categories of date display. These categories are based on the language used by your computer. For example, most user computers that reside in the United States use a standard known as US English. This commands how the date displays in the continental US. Each category uses specific characters to represent its value:

Format Description
MM The double M (in uppercase) string gets the numeric value of the month from 1 to 12. If the number is less than 10, it would display with the leading 0. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime date = new DateTime(2002, 4, 22);
            string strMonth = date.ToString("MM");

            Console.WriteLine("Date and Time: {0}", date);
            Console.WriteLine("Month Value:   {0}\n", strMonth);
            return 0;
        }
    }
}

This would produce:

Date and Time: 4/22/2002 12:00:00 AM
Month Value:   04

Press any key to continue . . .

It the number is higher than 9, it would display so.

MMM The triple M as MMM (in uppercase) gets the name of the month using three letters. This variable is defined by the operating system. The names of the month in US English are Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, and Dec. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime date = new DateTime(1998, 1, 12);
            string strMonth = date.ToString("MMM");

            Console.WriteLine("Date and Time: {0}", date);
            Console.WriteLine("Month Name:    {0}\n", strMonth);
            return 0;
        }
    }
}

This would produce:

Date and Time: 1/12/1998 12:00:00 AM
Month Name:    Jan

Press any key to continue . . .
MMMM The quadruple M as MMMM (in uppercase) gets the complete name of a month as defined by the operating system of the user's computer. The names of the months are January, February, March, April, May, June, July, August, September, October, November, and December.
Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime date = new DateTime(2004, 10, 23);
            string strMonth = date.ToString("MMMM");

            Console.WriteLine("Date and Time: {0}", date);
            Console.WriteLine("Month Name:    {0}\n", strMonth);
            return 0;
        }
    }
}

This would produce:

Date and Time: 10/23/2004 12:00:00 AM
Month Name:    October

Press any key to continue . . .
dd The double d gets the numeric day of the month. 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 date = new DateTime(2002, 4, 2);
            string strDay = date.ToString("dd");

            Console.WriteLine("Date and Time: {0}", date);
            Console.WriteLine("Month Value:   {0}\n", strDay);
            return 0;
        }
    }
}

This would display:

Date and Time: 4/2/2002 12:00:00 AM
Month Value:   02

Press any key to continue . . .
yy The double y is used to get the numeric year with the last two digits. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime date = new DateTime(2002, 4, 2);
            string strYear2Digits = date.ToString("yy");

            Console.WriteLine("Date and Time: {0}", date);
            Console.WriteLine("Year Value:    {0}\n", strYear2Digits);
            return 0;
        }
    }
}

This would produce:

Date and Time: 4/2/2002 12:00:00 AM
Year Value:    02

Press any key to continue . . .
yyyy The yyyy string is used to get all four digits of a year. Here is an example:
using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime date = new DateTime(2002, 4, 2);
            string strYear4Digits = date.ToString("yyyy");

            Console.WriteLine("Date and Time: {0}", date);
            Console.WriteLine("Year Value:    {0}\n", strYear4Digits);
            return 0;
        }
    }
}

This would produce:

Date and Time: 4/2/2002 12:00:00 AM
Year Value:    2002

Press any key to continue . . .

If the year was provided with two digits, such as 98, it would still be produced with 4 digits. Consider the following example:

using System;

namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime date = new DateTime(98, 4, 2);
            string strYear4Digits = date.ToString("yyyy");

            Console.WriteLine("Date and Time: {0}", date);
            Console.WriteLine("Year Value:    {0}\n", strYear4Digits);
            return 0;
        }
    }
}

This would produce:

Date and Time: 4/2/0098 12:00:00 AM
Year Value:    0098

Press any key to continue . . .

Notice that this may be a wrong date. For this reason, in your C# applications, you should always make it a habit to (always) provide your years with 4 digits.

Compilers  (as done in different implementations of various languages (C++, Pascal, Visual Basic, Java, SQL, Oracle, DB2, etc)), libraries (Win32, VCL, MFC, etc), applications (Microsoft Excel, OpenOffice.Org, Microsoft Access, Sun StarOffice, Quattro Pro, etc), and programming environments (Borland Developer Studio) have different ways of performing this operation. For example, in Microsoft Excel, if you are using the computer in the year 2006, if you provide a date value that has a year set with two digits such as 98, Microsoft Excel would add the two digits of the previous century, in this 19, to produce 1998. If you provide a year such as 02, the Microsoft Excel interpreter would add the current century to produce 2002.
 

Previous Copyright © 2006 FunctionX, Inc. Next