<p style="line-height:22px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px; font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left"> <strong></strong></p>
<pre class="blockcode"><code class="language-csharp">using System;
using System.Globalization;
public class SamplesDTFI {
public static void Main() {
// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false ).DateTimeFormat;
// Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
// The Gregorian calendar is the default calendar for the en-US culture.
DateTime myDT = new DateTime( 2002, 1, 3 );
// Displays the format pattern associated with each format character.
Console.WriteLine( "FORMAT en-US EXAMPLE" );
Console.WriteLine( "CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY\n" );
Console.WriteLine( " d {0}", myDT.ToString("d", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.ShortDatePattern, "(ShortDatePattern)" );
Console.WriteLine( " D {0}", myDT.ToString("D", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.LongDatePattern, "(LongDatePattern)" );
Console.WriteLine( " f {0}\n", myDT.ToString("f", myDTFI) );
Console.WriteLine( " F {0}", myDT.ToString("F", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.FullDateTimePattern, "(FullDateTimePattern)" );
Console.WriteLine( " g {0}\n", myDT.ToString("g", myDTFI) );
Console.WriteLine( " G {0}\n", myDT.ToString("G", myDTFI) );
Console.WriteLine( " m {0}", myDT.ToString("m", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
Console.WriteLine( " M {0}", myDT.ToString("M", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
Console.WriteLine( " o {0}\n", myDT.ToString("o", myDTFI) );
Console.WriteLine( " r {0}", myDT.ToString("r", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
Console.WriteLine( " R {0}", myDT.ToString("R", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
Console.WriteLine( " s {0}", myDT.ToString("s", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)" );
Console.WriteLine( " t {0}", myDT.ToString("t", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.ShortTimePattern, "(ShortTimePattern)" );
Console.WriteLine( " T {0}", myDT.ToString("T", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.LongTimePattern, "(LongTimePattern)" );
Console.WriteLine( " u {0}", myDT.ToString("u", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" );
Console.WriteLine( " U {0}\n", myDT.ToString("U", myDTFI) );
Console.WriteLine( " y {0}", myDT.ToString("y", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
Console.WriteLine( " Y {0}", myDT.ToString("Y", myDTFI) );
Console.WriteLine( " {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
}
}
/*
This code produces the following output.
FORMAT en-US EXAMPLE
CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY
d 1/3/2002
M/d/yyyy (ShortDatePattern)
D Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f Thursday, January 03, 2002 12:00 AM
F Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g 1/3/2002 12:00 AM
G 1/3/2002 12:00:00 AM
m January 03
MMMM dd (MonthDayPattern)
M January 03
MMMM dd (MonthDayPattern)
o 2002-01-03T00:00:00.0000000
r Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s 2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t 12:00 AM
h:mm tt (ShortTimePattern)
T 12:00:00 AM
h:mm:ss tt (LongTimePattern)
u 2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U Thursday, January 03, 2002 8:00:00 AM
y January, 2002
MMMM, yyyy (YearMonthPattern)
Y January, 2002
MMMM, yyyy (YearMonthPattern)
*/
</code></pre>
<p></p>
<p><strong>在平时编码中,经常要把日期转换成各种各样的形式输出或保 |
|