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


C# Convert.ToDateTime(String, IFormatProvider)用法及代碼示例


此方法用於使用指定的區域性特定格式信息將數字的指定字符串表示形式轉換為等效的日期和時間。

用法:

public static DateTime ToDateTime (string value, IFormatProvider provider);

參數:


  • value:包含要轉換的日期和時間的字符串。
  • provider:提供文化專用格式信息的對象。

返回值:此方法返回與value值等效的日期和時間,或者如果Min為null,則返回MinValue的日期和時間。

異常:如果該值不是正確格式的日期和時間字符串,則此方法將提供FormatException。

以下示例程序旨在說明Convert.ToDateTime(String,IFormatProvider)方法的使用

範例1:

// C# program to demonstrate the 
// Convert.ToDateTime() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
// Main Method 
public static void Main() 
{ 
    try { 
          
        // creating object of CultureInfo 
        CultureInfo cultures = new CultureInfo("en-US"); 
  
        // declaring and intializing String array 
        string[] values = {"01/02/09", "2009/02/03", 
            "01/2009/03", "01/02/2009", "01/02/23"}; 
  
        // calling get() Method 
        Console.WriteLine("Converted string value"+ 
                       " to specified DateTime:"); 
                         
        for (int j = 0; j < values.Length; j++) { 
              
            get(values[j], cultures); 
        } 
    } 
      
    catch (FormatException e) { 
          
        Console.WriteLine("\n"); 
        Console.Write("Exception Thrown:"); 
        Console.Write("{0}", e.GetType(), e.Message); 
    } 
} 
  
// Defining get() method 
public static void get(string s,  
           CultureInfo cultures) 
{ 
  
    // converting string to specified bool 
    DateTime val = Convert.ToDateTime(s, cultures); 
  
    // display the converted string 
    Console.Write(" {0}, ", val); 
} 
}
輸出:

Converted string value to specified DateTime:
01/02/2009 00:00:00, 02/03/2009 00:00:00, 01/03/2009 00:00:00, 01/02/2009 00:00:00, 01/02/2023 00:00:00,

範例2:對於FormatException

// C# program to demonstrate the 
// Convert.ToDateTime() Method 
using System; 
using System.Globalization; 
class GFG { 
  
// Main Method 
public static void Main() 
{ 
    try { 
          
        // creating object of CultureInfo 
        CultureInfo cultures = new CultureInfo("en-US"); 
  
        // declaring and intializing String array 
        string[] values = {"01/02/09", "2009/02/03",  
            "01/2009/03", "01/02/2009", "01/02/23"}; 
  
        // calling get() Method 
        Console.WriteLine("Converted string value "+ 
                         "to specified DateTime:"); 
                           
        for (int j = 0; j < values.Length; j++) { 
              
            get(values[j], cultures); 
        } 
        Console.WriteLine("\n\nvalue is not a properly "+ 
                      "formatted date and time string."); 
                        
        // converting string to specified bool 
        DateTime val = Convert.ToDateTime("21/2009/03", 
                                             cultures); 
  
        // display the converted string 
        Console.Write(" {0}, ", val); 
    } 
    catch (FormatException e) { 
        Console.Write("Exception Thrown:"); 
        Console.Write("{0}", e.GetType(), e.Message); 
    } 
} 
// Defining get() method 
public static void get(string s, 
           CultureInfo cultures) 
{ 
  
    // converting string to specified bool 
    DateTime val = Convert.ToDateTime(s, cultures); 
  
    // display the converted string 
    Console.Write(" {0}, ", val); 
} 
}
輸出:

Converted string value to specified DateTime:
01/02/2009 00:00:00, 02/03/2009 00:00:00, 01/03/2009 00:00:00, 01/02/2009 00:00:00, 01/02/2023 00:00:00,

value is not a properly formatted date and time string.
Exception Thrown:System.FormatException

參考:



相關用法


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