本文整理匯總了C#中System.DateTime.ToShortTimeString方法的典型用法代碼示例。如果您正苦於以下問題:C# DateTime.ToShortTimeString方法的具體用法?C# DateTime.ToShortTimeString怎麽用?C# DateTime.ToShortTimeString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.DateTime
的用法示例。
在下文中一共展示了DateTime.ToShortTimeString方法的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");
}
}
輸出:
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.ToShortTimeString()
//引入命名空間
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.ToLongTimeString() = " + myDateTime11.ToLongTimeString());
Console.WriteLine("myDateTime11.ToShortTimeString() = " + myDateTime11.ToShortTimeString());
}
}