ASP.NET C# 日期 时间 年 月 日 时 分 秒 格式及转换

论坛 期权论坛     
选择匿名的用户   2021-5-22 15:01   105   0
<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 &#61; new CultureInfo( &#34;en-US&#34;, false ).DateTimeFormat;

      // Creates a DateTime with the Gregorian date January 3, 2002 (year&#61;2002, month&#61;1, day&#61;3).
      // The Gregorian calendar is the default calendar for the en-US culture.
      DateTime myDT &#61; new DateTime( 2002, 1, 3 );

      // Displays the format pattern associated with each format character.
      Console.WriteLine( &#34;FORMAT  en-US EXAMPLE&#34; );
      Console.WriteLine( &#34;CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n&#34; );
      Console.WriteLine( &#34;  d     {0}&#34;, myDT.ToString(&#34;d&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.ShortDatePattern, &#34;(ShortDatePattern)&#34; );
      Console.WriteLine( &#34;  D     {0}&#34;, myDT.ToString(&#34;D&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.LongDatePattern, &#34;(LongDatePattern)&#34; );
      Console.WriteLine( &#34;  f     {0}\n&#34;, myDT.ToString(&#34;f&#34;, myDTFI) );
      Console.WriteLine( &#34;  F     {0}&#34;, myDT.ToString(&#34;F&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.FullDateTimePattern, &#34;(FullDateTimePattern)&#34; );
      Console.WriteLine( &#34;  g     {0}\n&#34;, myDT.ToString(&#34;g&#34;, myDTFI) );
      Console.WriteLine( &#34;  G     {0}\n&#34;, myDT.ToString(&#34;G&#34;, myDTFI) );
      Console.WriteLine( &#34;  m     {0}&#34;, myDT.ToString(&#34;m&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.MonthDayPattern, &#34;(MonthDayPattern)&#34; );
      Console.WriteLine( &#34;  M     {0}&#34;, myDT.ToString(&#34;M&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.MonthDayPattern, &#34;(MonthDayPattern)&#34; );
      Console.WriteLine( &#34;  o     {0}\n&#34;, myDT.ToString(&#34;o&#34;, myDTFI) );
      Console.WriteLine( &#34;  r     {0}&#34;, myDT.ToString(&#34;r&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.RFC1123Pattern, &#34;(RFC1123Pattern)&#34; );
      Console.WriteLine( &#34;  R     {0}&#34;, myDT.ToString(&#34;R&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.RFC1123Pattern, &#34;(RFC1123Pattern)&#34; );
      Console.WriteLine( &#34;  s     {0}&#34;, myDT.ToString(&#34;s&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.SortableDateTimePattern, &#34;(SortableDateTimePattern)&#34; );
      Console.WriteLine( &#34;  t     {0}&#34;, myDT.ToString(&#34;t&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.ShortTimePattern, &#34;(ShortTimePattern)&#34; );
      Console.WriteLine( &#34;  T     {0}&#34;, myDT.ToString(&#34;T&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.LongTimePattern, &#34;(LongTimePattern)&#34; );
      Console.WriteLine( &#34;  u     {0}&#34;, myDT.ToString(&#34;u&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.UniversalSortableDateTimePattern, &#34;(UniversalSortableDateTimePattern)&#34; );
      Console.WriteLine( &#34;  U     {0}\n&#34;, myDT.ToString(&#34;U&#34;, myDTFI) );
      Console.WriteLine( &#34;  y     {0}&#34;, myDT.ToString(&#34;y&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.YearMonthPattern, &#34;(YearMonthPattern)&#34; );
      Console.WriteLine( &#34;  Y     {0}&#34;, myDT.ToString(&#34;Y&#34;, myDTFI) );
      Console.WriteLine( &#34;        {0} {1}\n&#34;, myDTFI.YearMonthPattern, &#34;(YearMonthPattern)&#34; );

   }

}

/*
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&#39;:&#39;mm&#39;:&#39;ss &#39;GMT&#39; (RFC1123Pattern)

  R     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH&#39;:&#39;mm&#39;:&#39;ss &#39;GMT&#39; (RFC1123Pattern)

  s     2002-01-03T00:00:00
        yyyy&#39;-&#39;MM&#39;-&#39;dd&#39;T&#39;HH&#39;:&#39;mm&#39;:&#39;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&#39;-&#39;MM&#39;-&#39;dd HH&#39;:&#39;mm&#39;:&#39;ss&#39;Z&#39; (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>在平时编码中,经常要把日期转换成各种各样的形式输出或保
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP