C# 中的 DateTime.ToShortDateString() 方法
C# 中的 DateTime.ToShortDateString() 方法用于将当前 DateTime 对象的值转换为其等效的短日期字符串表示形式。
语法
语法如下 −
public string ToShortDateString ();
示例
现在,让我们看一个示例来实现 DateTime.ToShortDateString() 方法 −
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2019, 08, 11, 9, 10, 45); Console.WriteLine("Date = {0}", d); string str = d.ToShortDateString(); Console.WriteLine("Short date string representation = {0}", str); } }
输出
这将产生以下输出 −
Date = 8/11/2019 9:10:45 AM Short date string representation = 8/11/2019
示例
现在,让我们看另一个示例来实现 DateTime.ToShortDateString() 方法 −
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.ToShortDateString(); Console.WriteLine("Short date string = {0}", pattern.ShortDatePattern); Console.WriteLine("Short date string representation = {0}", str); } }
输出
这将产生以下输出 −
Date = 10/16/2019 8:56:40 AM Current culture = en-US Short date string = M/d/yyyy Short date string representation = 10/16/2019
广告