當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# DateTime.GetDateTimeFormats()方法用法及代碼示例

此方法用於將該實例的值轉換為標準日期和時間格式說明符支持的所有字符串表示形式。此方法的重載列表中共有4種方法:

  • GetDateTimeFormats()
  • GetDateTimeFormats(Char)
  • GetDateTimeFormats(IFormatProvider)
  • GetDateTimeFormats(Char, IFormatProvider)

GetDateTimeFormats()

此方法用於將該實例的值轉換為標準日期和時間格式說明符支持的所有字符串表示形式。

用法: public string[] GetDateTimeFormats ()


返回值:此方法返回一個字符串數組,其中每個元素都是用標準日期和時間格式說明符之一格式化的實例的值的表示形式。

以下示例程序旨在說明GetDateTimeFormats()方法的使用:

範例1:

// C# program to demonstrate the 
// DateTime.GetDateTimeFormats() 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // creating object of DateTime 
        DateTime date = new DateTime(2010, 1, 
                                     1, 4, 0, 15); 
  
        // getting format in string array 
        // using GetDateTimeFormats() method; 
        string[] value = date.GetDateTimeFormats(); 
  
        // Print out value in all DateTime  
        // formats using the default culture. 
        foreach(string format in value) 
            Console.WriteLine(format); 
    } 
}
輸出:
01/01/2010
2010-01-01
Friday, 01 January 2010
Friday, 01 January 2010 04:00
Friday, 01 January 2010 04:00 AM
Friday, 01 January 2010 4:00
Friday, 01 January 2010 4:00 AM
Friday, 01 January 2010 04:00:15
01/01/2010 04:00
01/01/2010 04:00 AM
01/01/2010 4:00
01/01/2010 4:00 AM
2010-01-01 04:00
2010-01-01 04:00 AM
2010-01-01 4:00
2010-01-01 4:00 AM
01/01/2010 04:00:15
2010-01-01 04:00:15
January 01
January 01
2010-01-01T04:00:15.0000000
2010-01-01T04:00:15.0000000
Fri, 01 Jan 2010 04:00:15 GMT
Fri, 01 Jan 2010 04:00:15 GMT
2010-01-01T04:00:15
04:00
04:00 AM
4:00
4:00 AM
04:00:15
2010-01-01 04:00:15Z
Friday, 01 January 2010 04:00:15
2010 January
2010 January

範例2:

// C# program to demonstrate the 
// DateTime.GetDateTimeFormats() 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // creating object of DateTime 
        DateTime date = new DateTime(2019, 1, 
                              30, 9, 49, 15); 
  
        // getting format in string array 
        // using GetDateTimeFormats() method; 
        string[] value = date.GetDateTimeFormats(); 
  
        // Print out value in all DateTime  
        // formats using the default culture. 
        for (int i = 1; i <= 6; i++) 
            Console.WriteLine(value[i]); 
    } 
}
輸出:
2019-01-30
Wednesday, 30 January 2019
Wednesday, 30 January 2019 09:49
Wednesday, 30 January 2019 09:49 AM
Wednesday, 30 January 2019 9:49
Wednesday, 30 January 2019 9:49 AM

GetDateTimeFormats(Char)

此方法用於將該實例的值轉換為指定的標準日期和時間格式說明符支持的所有字符串表示形式。

用法: public string[] GetDateTimeFormats (char format);
Here it takes a standard date and time format string.

返回值:此方法返回一個字符串數組,其中每個元素都是用標準日期和時間格式說明符格式化的實例的值的表示形式。

異常:如果格式不是有效的標準日期和時間格式說明符,則此方法將提供FormatException。



以下示例程序旨在說明GetDateTimeFormats(Char)方法的用法:

範例1:

// C# program to demonstrate the 
// DateTime.GetDateTimeFormats(Char) 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
            // creating object of DateTime 
            DateTime date = new DateTime(2010, 1, 
                                     1, 4, 0, 15); 
  
            // Get the long date formats using the current 
            // culture. using GetDateTimeFormats() method 
            string[] value = date.GetDateTimeFormats('D'); 
  
            // Print out value in all DateTime 
            // formats using the default culture. 
            foreach(string format in value) 
                Console.WriteLine(format); 
        } 
        catch (FormatException e) { 
            Console.Write("Exception Thrown:"); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
Friday, 01 January 2010

範例2:對於FormatException

// C# program to demonstrate the 
// DateTime.GetDateTimeFormats(Char) 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
            // creating object of DateTime 
            DateTime date = new DateTime(2010, 1, 
                                         1, 4, 0, 15); 
  
            // Get the date format 
            // using GetDateTimeFormats(Char) method; 
            string[] value = date.GetDateTimeFormats('X'); 
  
            // Print out value in all DateTime  
            // formats using the default culture. 
            foreach(string format in value) 
                Console.WriteLine(format); 
        } 
        catch (FormatException e) { 
            Console.Write("Exception Thrown:"); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
Exception Thrown:System.FormatException

參考:




相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 DateTime.GetDateTimeFormats() Method in C# | Set – 1。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。