DateTime.ToShortTimeString() 方法在 C# 中
C# 中的 DateTime.ToShortTimeString() 方法用于将当前 DateTime 对象的值转换为等效的短时间字符串表示形式。
语法
以下是语法 −
public string ToShortTimeString ();
示例
现在让我们看一个示例来实现 DateTime.ToShortTimeString() 方法 −
using System; using System.Globalization; public class Demo { public static void Main() { DateTime d = DateTime.Now; Console.WriteLine("Date = {0}", d); Console.WriteLine("Current culture = "+CultureInfo.CurrentCulture.Name); var pattern = CultureInfo.CurrentCulture.DateTimeFormat; string str = d.ToShortTimeString(); Console.WriteLine("Short time string = {0}", pattern.ShortTimePattern); Console.WriteLine("Short time string representation = {0}", str); } }
输出
这将产生以下输出 −
Date = 10/16/2019 8:59:23 AM Current culture = en-US Short time string = h:mm tt Short time string representation = 8:59 AM
示例
现在让我们看另一个示例来实现 DateTime.ToShortTimeString() 方法 −
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2019, 11, 11, 7, 11, 25); Console.WriteLine("Date = {0}", d); string str = d.ToShortTimeString(); Console.WriteLine("Short time string representation = {0}", str); } }
输出
这将产生以下输出 −
Date = 11/11/2019 7:11:25 AM Short time string representation = 7:11 AM
广告