You need help with DateTime formatting strings in the C# language or other .NET languages. The framework provides powerful formatting capabilities, but the syntax is confusing and there are some tricks. Here we see examples of using DateTime formats, and also the different values you can get with the individual formats.

Format your DateTimes in the best way for your application.
You will not have to write elaborate custom routines.
The .NET Framework has a powerful DateTime format mechanism.

Using DateTime format string

Here we see an example of how you can use a specific formatting string with DateTime and ToString to obtain a special DateTime string. This is useful when interacting with other systems, or when you require a precise format.

=== Program that uses DateTime format (C#) ===

 

using System;

class Program
{
static void Main()
{
DateTime time = DateTime.Now; // Use current time
string format = "MMM ddd d HH:mm yyyy"; // Use this format
Console.WriteLine(time.ToString(format)); // Write to console
}
}

=== Output of the program ===

Feb Fri 27 11:41 2009

=== Format string pattern ===

MMM display three-letter month
ddd display three-letter day of the WEEK
d display day of the MONTH
HH display two-digit hours on 24-hour scale
mm display two-digit minutes
yyyy display four-digit year

Notes on the letters. The letters in the format string above specify the output you want to display. The final comment shows what the MMM, ddd, d, HH, mm, and yyyy will do.

Modifying DateTime format string

Here we see how you can modify the DateTime format string in the above example to get different output with ToString. We change some of the fields so the resulting value is shorter.

=== Program that uses different format (C#) ===

 

using System;

class Program
{
static void Main()
{
DateTime time = DateTime.Now; // Use current time
string format = "M d h:mm yy"; // Use this format
Console.WriteLine(time.ToString(format)); // Write to console
}
}

=== Output of the program ===

2 27 11:48 09

=== Format string pattern ===

M display one-digit month number [changed]
d display one-digit day of the MONTH [changed]
h display one-digit hour on 12-hour scale [changed]
mm display two-digit minutes
yy display two-digit year [changed]

Note on format string usages. You will also need to specify a format string when using DateTime.ParseExact and DateTime.ParseExact. This is because those methods require a custom pattern to parse.

Single-letter DateTime format strings

Here we see that you can use a single character with ToString or DateTime.ParseExact to specify a preset format available in the framework. These are standard formats and very useful in many programs. They can eliminate typos in the custom format strings.

=== Program that tests formats (C#) ===

 

using System;

class Program
{
static void Main()
{
DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("d"));
Console.WriteLine(now.ToString("D"));
Console.WriteLine(now.ToString("f"));
Console.WriteLine(now.ToString("F"));
Console.WriteLine(now.ToString("g"));
Console.WriteLine(now.ToString("G"));
Console.WriteLine(now.ToString("m"));
Console.WriteLine(now.ToString("M"));
Console.WriteLine(now.ToString("o"));
Console.WriteLine(now.ToString("O"));
Console.WriteLine(now.ToString("s"));
Console.WriteLine(now.ToString("t"));
Console.WriteLine(now.ToString("T"));
Console.WriteLine(now.ToString("u"));
Console.WriteLine(now.ToString("U"));
Console.WriteLine(now.ToString("y"));
Console.WriteLine(now.ToString("Y"));
}
}

=== Output of the program ===

d 2/27/2009
D Friday, February 27, 2009
f Friday, February 27, 2009 12:11 PM
F Friday, February 27, 2009 12:12:22 PM
g 2/27/2009 12:12 PM
G 2/27/2009 12:12:22 PM
m February 27
M February 27
o 2009-02-27T12:12:22.1020000-08:00
O 2009-02-27T12:12:22.1020000-08:00
s 2009-02-27T12:12:22
t 12:12 PM
T 12:12:22 PM
u 2009-02-27 12:12:22Z
U Friday, February 27, 2009 8:12:22 PM
y February, 2009
Y February, 2009

ToLongDateString and ToShortDateString

Here we see the ToLongDateString, ToLongTimeString, ToShortDateString, and ToShortTimeString methods on DateTime. These methods are equivalent to the lowercase and uppercase D and T methods shown in the example above.

=== Program that uses ToString methods (C#) ===

 

using System;

class Program
{
static void Main()
{
DateTime now = DateTime.Now;
Console.WriteLine(now.ToLongDateString()); // Equivalent to D
Console.WriteLine(now.ToLongTimeString()); // Equivalent to T
Console.WriteLine(now.ToShortDateString()); // Equivalent to d
Console.WriteLine(now.ToShortTimeString()); // Equivalent to t
Console.WriteLine(now.ToString());
}
}

=== Output of the program ===

ToLongDateString Friday, February 27, 2009
ToLongTimeString 12:16:59 PM
ToShortDateString 2/27/2009
ToShortTimeString 12:16 PM
ToString 2/27/2009 12:16:59 PM

 

摘自 http://dotnetperls.com/datetime-format

posted on 2010-11-30 14:27  露水丛生  阅读(326)  评论(0编辑  收藏  举报