C# 中的 DateTime.GetDateTimeFormats() 方法


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

更新于: 2019-11-08

983 次浏览

开启你的 职业生涯

完成课程即可获得认证

开始吧
广告