📌  相关文章
📜  C#| Convert.ToDateTime(String,IFormatProvider)方法

📅  最后修改于: 2021-05-29 16:45:06             🧑  作者: Mango

此方法用于使用指定的区域性特定格式信息将数字的指定字符串表示形式转换为等效的日期和时间。

句法:

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

参数:

  • value :一个字符串,其中包含要转换的日期和时间。
  • provider :提供特定于区域性格式信息的对象。

返回值:此方法返回与value等效的日期和时间,或者返回与MinValue等效的日期和时间(如果值为null)

异常:如果该值不是正确格式化的日期和时间字符串,则此方法将给出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);
}
}
输出:

示例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);
}
}
输出:

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.convert.todatetime?view=netframework-4.7.2#System_Convert_ToDateTime_System_String_System_IFormatProvider_