Formatting Date and Time


In addition to formatting numeric values, another data type to which formatting is often applied is DateTime. DateTime represents date and time. Date and time values can be displayed a variety of ways. Here are just a few examples:

  • 06/05/2006

  • Monday, June 5, 2006

  • 12:59:00

  • 12:59:00 PM

Also, the date and time representations can vary from country to country. For these reasons, C# provides an extensive formatting subsystem for time and date values.

Date and time formatting is handled through format specifiers. The format specifiers for date and time are shown in Table 21-7. Because the specific date and time representation may vary from country to country and by language, the precise representation generated will be influenced by the cultural settings of the computer.

Table 21-7: The Date and Time Format Specifiers

Specifier

Format

D

Date in long form.

d

Date in short form.

F

Date and time in long form.

f

Date and time in short form.

G

Date in short form, time in long form.

g

Date in short form, time in short form.

M

Month and day.

m

Same as M.

R

Date and time in standard, GMT form.

r

Same as R.

s

A sortable form of date and time.

T

Time in long form.

t

Time in short form.

U

Long form, universal form of date and time. Time is displayed as UTC.

u

Short form, universal form of date and time.

Y

Month and year.

y

Same as Y.

Here is a program that demonstrates the date and time format specifiers:

 // Format time and date information. using System; class TimeAndDateFormatDemo {   public static void Main() {     DateTime dt = DateTime.Now; // obtain current time     Console.WriteLine("d format: {0:d}", dt);     Console.WriteLine("D format: {0:D}", dt);     Console.WriteLine("t format: {0:t}", dt);     Console.WriteLine("T format: {0:T}", dt);     Console.WriteLine("f format: {0:f}", dt);     Console.WriteLine("F format: {0:F}", dt);     Console.WriteLine("g format: {0:g}", dt);     Console.WriteLine("G format: {0:G}", dt);     Console.WriteLine("m format: {0:m}", dt);     Console.WriteLine("M format: {0:M}", dt);     Console.WriteLine("r format: {0:r}", dt);     Console.WriteLine("R format: {0:R}", dt);     Console.WriteLine("s format: {0:s}", dt);     Console.WriteLine("u format: {0:u}", dt);     Console.WriteLine("U format: {0:U}", dt);     Console.WriteLine("y format: {0:y}", dt);     Console.WriteLine("Y format: {0:Y}", dt);   } }

Sample output is shown here:

 d format: 6/5/2006 D format: Monday, June 05, 2006 t format: 3:24 PM T format: 3:24:14 PM f format: Monday, June 05, 2006 3:24 PM F format: Monday, June 05, 2006 3:24:14 PM g format: 6/5/2006 3:24 PM G format: 6/5/2006 3:24:14 PM m format: June 05 M format: June 05 r format: Mon, 05 Jun 2006 15:24:14 GMT R format: Mon, 05 Jun 2006 15:24:14 GMT s format: 2006-06-05T15:24:14 u format: 2006-06-05 15:24:14Z U format: Monday, June 05, 2006 8:24:14 PM y format: June, 2006 Y format: June, 2006

The next program creates a very simple clock. The time is updated once every second. At the top of each hour, the computer’s bell is sounded. It uses the ToString( ) method of DateTime to obtain the formatted time prior to outputting it. If the top of the hour has been reached, then the alert character (\a) is appended to the formatted time, thus ringing the bell.

 // A simple clock. using System; class SimpleClock {   public static void Main() {     string t;     int seconds;     DateTime dt = DateTime.Now;     seconds = dt.Second;     for(;;) {       dt = DateTime.Now;       // update time if seconds change       if(seconds != dt.Second) {         seconds = dt.Second;         t = dt.ToString("T");         if(dt.Minute==0 && dt.Second==0)           t = t + "\a"; // ring bell at top of hour         Console.WriteLine(t);       }     }   } }

Creating a Custom Date and Time Format

Although the standard date and time format specifiers will apply to the vast majority of situations, you can create your own, custom formats. The process is similar to creating custom formats for the numeric types, as described earlier. In essence, you simply create an example (picture) of what you want the date and time information to look like. To create a custom date and time format, you will use one or more of the placeholders shown in Table 21-8.

Table 21-8: The Custom Date and Time Placeholder Characters

Placeholder

Replaced By

d

Day of month as a number between 1 and 31.

dd

Day of month as a number between 1 and 31. A leading zero prefixes the values 1 through 9.

ddd

Abbreviated weekday name.

dddd

Full weekday name.

f, ff, fff, ffff, fffff, ffffff, fffffff

Fractional seconds, with the number of decimal places specified by the number of fs. If capital Fs are used, then trailing zeros are not displayed.

g

Era.

h

Hour as a number between 1 and 12.

hh

Hour as a number between 1 and 12. A leading zero prefixes the values 1 through 9.

H

Hour as a number between 0 and 23.

HH

Hour as a number between 0 and 23. A leading zero prefixes the values 0 through 9.

m

Minutes.

mm

Minutes. A leading zero prefixes the values 0 through 9.

M

Month as a number between 1 and 12.

MM

Month as a number between 1 and 12. A leading zero prefixes the values 1 through 9.

MMM

Abbreviated month name.

MMMM

Full month name.

s

Seconds.

ss

Seconds. A leading zero prefixes the values 0 through 9.

t

A or P, indicating A.M. or P.M.

tt

A.M. or P.M.

y

Year as two digits, unless only one digit is needed.

yy

Year as two digits. A leading zero prefixes the values 0 through 9.

yyyy

Year using four digits.

z

Time zone offset in hours.

zz

Time zone offset in hours. A leading zero prefixes the values 0 through 9.

zzz

Time zone offset in hours and minutes.

:

Separator for time components.

/

Separator for date components.

%fmt

The standard format associated with fmt.

If you examine Table 21-8, you will see that the placeholders d, f, g, m, M, s, and t are the same as the date and time format specifiers shown in Table 21-7. In general, if one of these characters is used by itself, it is interpreted as a format specifier. Otherwise, it is assumed to be a placeholder. If you want use one of these characters by itself but have it interpreted as a placeholder, then precede the character with a %.

The following program demonstrates several custom time and date formats:

 // Format time and date information. using System; class CustomTimeAndDateFormatsDemo {   public static void Main() {     DateTime dt = DateTime.Now;     Console.WriteLine("Time is {0:hh:mm tt}", dt);     Console.WriteLine("24 hour time is {0:HH:mm}", dt);     Console.WriteLine("Date is {0:ddd MMM dd, yyyy}", dt);     Console.WriteLine("Era: {0:gg}", dt);     Console.WriteLine("Time with seconds: " +                       "{0:HH:mm:ss tt}", dt);     Console.WriteLine("Use m for day of month: {0:m}", dt);     Console.WriteLine("use m for minutes: {0:%m}", dt);   } }

The output is shown here:

 Time is 03:24 PM 24 hour time is 15:24 Date is Mon Jun 05, 2006 Era: A.D. Time with seconds: 15:24:17 PM Use m for day of month: June 05 use m for minutes: 24




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net