C#中的DateTime.GetDateTimeFormats()方法用于将该实例的值转换为标准日期和时间格式说明符支持的所有字符串表示形式。
以下是语法-
public string[] GetDateTimeFormats () public string[] GetDateTimeFormats (char ch);
上面的ch是标准的日期和时间格式字符串。
现在让我们看一个实现DateTime.GetDateTimeFormats()方法的示例-
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2019, 11, 10, 7, 20, 45); string[] res = d.GetDateTimeFormats(); foreach(string s in res) Console.WriteLine(s); } }
输出结果
这将产生以下输出-
11/10/2019 11/10/19 11/10/19 11/10/2019 19/11/10 2019-11-10 10-Nov-19 Sunday, November 10, 2019 November 10, 2019 Sunday, 10 November, 2019 10 November, 2019 Sunday, November 10, 2019 7:20 AM Sunday, November 10, 2019 07:20 AM Sunday, November 10, 2019 7:20 Sunday, November 10, 2019 07:20 November 10, 2019 7:20 AM November 10, 2019 07:20 AM November 10, 2019 7:20 November 10, 2019 07:20 Sunday, 10 November, 2019 7:20 AM Sunday, 10 November, 2019 07:20 AM Sunday, 10 November, 2019 7:20 Sunday, 10 November, 2019 07:20 10 November, 2019 7:20 AM 10 November, 2019 07:20 AM 10 November, 2019 7:20 10 November, 2019 07:20 Sunday, November 10, 2019 7:20:45 AM Sunday, November 10, 2019 07:20:45 AM Sunday, November 10, 2019 7:20:45 Sunday, November 10, 2019 07:20:45 November 10, 2019 7:20:45 AM November 10, 2019 07:20:45 AM November 10, 2019 7:20:45 November 10, 2019 07:20:45 Sunday, 10 November, 2019 7:20:45 AM Sunday, 10 November, 2019 07:20:45 AM Sunday, 10 November, 2019 7:20:45 Sunday, 10 November, 2019 07:20:45 10 November, 2019 7:20:45 AM 10 November, 2019 07:20:45 AM 10 November, 2019 7:20:45 10 November, 2019 07:20:45 11/10/2019 7:20 AM 11/10/2019 07:20 AM 11/10/2019 7:20 11/10/2019 07:20 11/10/19 7:20 AM 11/10/19 07:20 AM 11/10/19 7:20 11/10/19 07:20 11/10/19 7:20 AM 11/10/19 07:20 AM 11/10/19 7:20 11/10/19 07:20 11/10/2019 7:20 AM 11/10/2019 07:20 AM 11/10/2019 7:20 11/10/2019 07:20 19/11/10 7:20 AM 19/11/10 07:20 AM 19/11/10 7:20 19/11/10 07:20 2019-11-10 7:20 AM 2019-11-10 07:20 AM 2019-11-10 7:20 2019-11-10 07:20 10-Nov-19 7:20 AM 10-Nov-19 07:20 AM 10-Nov-19 7:20 10-Nov-19 07:20 11/10/2019 7:20:45 AM 11/10/2019 07:20:45 AM 11/10/2019 7:20:45 11/10/2019 07:20:45 11/10/19 7:20:45 AM 11/10/19 07:20:45 AM 11/10/19 7:20:45 11/10/19 07:20:45 11/10/19 7:20:45 AM 11/10/19 07:20:45 AM 11/10/19 7:20:45 11/10/19 07:20:45 11/10/2019 7:20:45 AM 11/10/2019 07:20:45 AM 11/10/2019 7:20:45 11/10/2019 07:20:45 19/11/10 7:20:45 AM 19/11/10 07:20:45 AM 19/11/10 7:20:45 19/11/10 07:20:45 2019-11-10 7:20:45 AM 2019-11-10 07:20:45 AM 2019-11-10 7:20:45 2019-11-10 07:20:45 10-Nov-19 7:20:45 AM 10-Nov-19 07:20:45 AM 10-Nov-19 7:20:45 10-Nov-19 07:20:45 November 10 November 10 2019-11-10T07:20:45.0000000 2019-11-10T07:20:45.0000000 Sun, 10 Nov 2019 07:20:45 GMT Sun, 10 Nov 2019 07:20:45 GMT 2019-11-10T07:20:45 7:20 AM 07:20 AM 7:20 07:20 7:20:45 AM 07:20:45 AM 7:20:45 07:20:45 2019-11-10 07:20:45Z Sunday, November 10, 2019 7:20:45 AM Sunday, November 10, 2019 07:20:45 AM Sunday, November 10, 2019 7:20:45 Sunday, November 10, 2019 07:20:45 November 10, 2019 7:20:45 AM November 10, 2019 07:20:45 AM November 10, 2019 7:20:45 November 10, 2019 07:20:45 Sunday, 10 November, 2019 7:20:45 AM Sunday, 10 November, 2019 07:20:45 AM Sunday, 10 November, 2019 7:20:45 Sunday, 10 November, 2019 07:20:45 10 November, 2019 7:20:45 AM 10 November, 2019 07:20:45 AM 10 November, 2019 7:20:45 10 November, 2019 07:20:45 November 2019 November 2019
现在让我们来看另一个实现DateTime.GetDateTimeFormats()方法的示例。对于char格式,我们使用格式说明符,例如“ d”表示短日期模式,“ D”表示长日期模式,“ F”表示完整日期/时间模式(长时间),等等,
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2019, 11, 10, 7, 20, 45); string[] res = d.GetDateTimeFormats('F'); foreach(string s in res) Console.WriteLine(s); } }
输出结果
这将产生以下输出-
Sunday, November 10, 2019 7:20:45 AM Sunday, November 10, 2019 07:20:45 AM Sunday, November 10, 2019 7:20:45 Sunday, November 10, 2019 07:20:45 November 10, 2019 7:20:45 AM November 10, 2019 07:20:45 AM November 10, 2019 7:20:45 November 10, 2019 07:20:45 Sunday, 10 November, 2019 7:20:45 AM Sunday, 10 November, 2019 07:20:45 AM Sunday, 10 November, 2019 7:20:45 Sunday, 10 November, 2019 07:20:45 10 November, 2019 7:20:45 AM 10 November, 2019 07:20:45 AM 10 November, 2019 7:20:45 10 November, 2019 07:20:45