Date Formats |
|
The Computer’s System of Displaying Dates |
To display a date in an application, by default, the compiler checks the Regional Options of the Control Panel. To see the options, from the Control Panel, you can double-click Regional or Language Option. From the Regional Options tab of the Regional or Language Option dialog box, you can click Customize... In the Customize Regional Options dialog box, click Date. |
You may have notice that, by default, a DateTime object always produces both a date and a time. In some cases, you will be interested in only the date portion of the object. To get a date value, you can call the DateTime.ToString() method that takes a String as argument and apply some rules:
The other characters and their combinations (MM, MMM, MMMM, dd, yy, and yyyy) are used as we reviewed them. Here are examples of displaying date formats: using System; namespace DateAndTime { class Program { static int Main() { DateTime date = new DateTime(2004, 10, 23); Console.WriteLine("Date and Time: {0}", date); Console.WriteLine(date.ToString("M/d/yyyy")); Console.WriteLine(date.ToString("M/d/yy")); Console.WriteLine(date.ToString("MM/dd/yy")); Console.WriteLine(date.ToString("MM/dd/yyyy")); Console.WriteLine(date.ToString("yy/MM/dd")); Console.WriteLine(date.ToString("yyyy-MM-dd")); Console.WriteLine(date.ToString("dd-MMM-yy")); return 0; } } } This would produce: 10/15/2006 10/15/06 10/15/06 10/15/2006 06/10/15 2006-10-15 15-Oct-06 Press any key to continue . . .
Instead of creating your own format, the Microsoft Windows operating system provides two names that can be used to identify a date. A date is referred to as short if it includes (only) the numeric portions of the month and the day of a date value. The operating systems follows the rules we have reviewed so far for the numbers and the Date Separator. The possible formats of a short date can be seen in the Short Date Format combo box of the Date tab of the Customize Regional Options of the Regional and Language Settings of the Control Panel:
To get the Short Date of a DateTime object, pass a "d" (one d in lowercase) string to the ToString() method. Here is an example: using System; namespace DateAndTime { class Program { static int Main() { DateTime date = new DateTime(2004, 10, 23); string strDate = date.ToString("d"); Console.WriteLine("Date and Time: {0}", date); Console.WriteLine("Date Portion: {0}\n", strDate); return 0; } } } This would produce: Date and Time: 10/23/2004 12:00:00 AM Date Portion: 10/23/2004 Press any key to continue . . .
A date is referred to as long if it includes the names of the month and the day of the week of a date value. This is called the Long Date Format. To get the Long Date of a date, pass a "D" (one d in uppercase) string to the ToString() method of a DateTime object. Here is an example: using System; namespace DateAndTime { class Program { static int Main() { DateTime date = new DateTime(2004, 10, 23); string strDate = date.ToString("D"); Console.WriteLine("Date and Time: {0}", date); Console.WriteLine("Date Portion: {0}\n", strDate); return 0; } } } To produce the result, the compiler refers to the Long Date Format combo box of the Customize Regional Options of the Control Panel. The user can change the format by selecting one from the combo box:
Based on the default settings of a computer used in US English, the above program would produce: Date and Time: 10/23/2004 12:00:00 AM Date Portion: Saturday, October 23, 2004 Press any key to continue . . .
The .NET Framework provides other formats, not regularly used but available. To get the name of a month and the year value of a DateTime object, both separated by an empty space, pass a single M (uppercase) as string to the ToString() method of a DateTime object. Here is an example: using System; namespace DateAndTime { class Program { static int Main() { DateTime date = new DateTime(2004, 10, 23); string strDate = date.ToString("M"); Console.WriteLine("Date and Time: {0}", date); Console.WriteLine("Month and Year: {0}\n", strDate); return 0; } } } This would produce: Date and Time: 10/23/2004 12:00:00 AM Month and Year: October 23 Press any key to continue . . . To include a comma in the result, pass a single y (lowercase) as string to the ToString() method of a DateTime object. Here is an example: using System; namespace DateAndTime { class Program { static int Main() { DateTime date = new DateTime(2004, 10, 23); string strDate = date.ToString("y"); Console.WriteLine("Date and Time: {0}", date); Console.WriteLine("Month and Year: {0}\n", strDate); return 0; } } } This would produce: Date and Time: 10/23/2004 12:00:00 AM Month and Year: October, 2004 Press any key to continue . . . |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|