2011年8月7日 星期日

[C#] DateTime 日期格式



DateTime 表示時間的瞬間,通常以一天的日期和時間表示。
裡面是 DateTime ToString 的常用格式。



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") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.ShortDatePattern, "(ShortDatePattern)" );
      Console.WriteLine( "  D     {0}", myDT.ToString("D") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.LongDatePattern, "(LongDatePattern)" );
      Console.WriteLine( "  f     {0}\n", myDT.ToString("f") );
      Console.WriteLine( "  F     {0}", myDT.ToString("F") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.FullDateTimePattern, "(FullDateTimePattern)" );
      Console.WriteLine( "  g     {0}\n", myDT.ToString("g") );
      Console.WriteLine( "  G     {0}\n", myDT.ToString("G") );
      Console.WriteLine( "  m     {0}", myDT.ToString("m") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
      Console.WriteLine( "  M     {0}", myDT.ToString("M") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
      Console.WriteLine( "  r     {0}", myDT.ToString("r") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
      Console.WriteLine( "  R     {0}", myDT.ToString("R") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
      Console.WriteLine( "  s     {0}", myDT.ToString("s") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)" );
      Console.WriteLine( "  t     {0}", myDT.ToString("t") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.ShortTimePattern, "(ShortTimePattern)" );
      Console.WriteLine( "  T     {0}", myDT.ToString("T") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.LongTimePattern, "(LongTimePattern)" );
      Console.WriteLine( "  u     {0}", myDT.ToString("u") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" );
      Console.WriteLine( "  U     {0}\n", myDT.ToString("U") );
      Console.WriteLine( "  y     {0}", myDT.ToString("y") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
      Console.WriteLine( "  Y     {0}", myDT.ToString("Y") );
      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)

  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)

*/

沒有留言:

張貼留言