當前位置: 首頁>>代碼示例>>C#>>正文


C# DateTime.ToLongDateString方法代碼示例

本文整理匯總了C#中System.DateTime.ToLongDateString方法的典型用法代碼示例。如果您正苦於以下問題:C# DateTime.ToLongDateString方法的具體用法?C# DateTime.ToLongDateString怎麽用?C# DateTime.ToLongDateString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.DateTime的用法示例。


在下文中一共展示了DateTime.ToLongDateString方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
        // Initialize a DateTime object.
        Console.WriteLine("Initialize the DateTime object to May 16, 2001 3:02:15 AM.\n");
        DateTime dateAndTime = new System.DateTime(2001, 5, 16, 3, 2, 15);

        // Display the name of the current culture.
        Console.WriteLine($"Current culture: \"{CultureInfo.CurrentCulture.Name}\"\n");
        var dtfi = CultureInfo.CurrentCulture.DateTimeFormat;

        // Display the long date pattern and string.
        Console.WriteLine($"Long date pattern: \"{dtfi.LongDatePattern}\"");
        Console.WriteLine($"Long date string:  \"{dateAndTime.ToLongDateString()}\"\n");

        // Display the long time pattern and string.
        Console.WriteLine($"Long time pattern: \"{dtfi.LongTimePattern}\"");
        Console.WriteLine($"Long time string:  \"{dateAndTime.ToLongTimeString()}\"\n");

        // Display the short date pattern and string.
        Console.WriteLine($"Short date pattern: \"{dtfi.ShortDatePattern}\"");
        Console.WriteLine($"Short date string:  \"{dateAndTime.ToShortDateString()}\"\n");

        // Display the short time pattern and string.
        Console.WriteLine($"Short time pattern: \"{dtfi.ShortTimePattern}\"");
        Console.WriteLine($"Short time string:  \"{dateAndTime.ToShortTimeString()}\"\n");
    }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:33,代碼來源:DateTime.ToLongDateString

輸出:

Current culture: "en-US"

Long date pattern: "dddd, MMMM d, yyyy"
Long date string:  "Wednesday, May 16, 2001"

Long time pattern: "h:mm:ss tt"
Long time string:  "3:02:15 AM"

Short date pattern: "M/d/yyyy"
Short date string:  "5/16/2001"

Short time pattern: "h:mm tt"
Short time string:  "3:02 AM"

示例2: DateTime.ToLongDateString()

//引入命名空間
using System;

class MainClass
{
  public static void Main()
  {
    DateTime myDateTime11 = new DateTime(2004, 1, 15, 23, 2, 5);
    Console.WriteLine("myDateTime11 = " + myDateTime11);
    Console.WriteLine("myDateTime11.ToLongDateString() = " + myDateTime11.ToLongDateString());
    Console.WriteLine("myDateTime11.ToShortDateString() = " + myDateTime11.ToShortDateString());


  }
}
開發者ID:C#程序員,項目名稱:System,代碼行數:15,代碼來源:DateTime.ToLongDateString


注:本文中的System.DateTime.ToLongDateString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。