C# | Convert。今日时间(字符串,表单提供者)方法
原文:https://www . geesforgeks . org/c-sharp-convert-todaytetimestring-iformat provider-method/
此方法用于使用指定的区域性特定格式信息,将数字的指定字符串表示形式转换为等效的日期和时间。 语法:
public static DateTime ToDateTime (string value, IFormatProvider provider);
参数:
- 值:包含要转换的日期和时间的字符串。
- 提供程序:提供区域性特定格式信息的对象。
返回值:此方法返回值的日期时间等价物,如果值为空,则返回民值的日期时间等价物。 异常:如果值不是格式正确的日期和时间字符串,此方法将给出格式异常。 以下程序说明了转换的使用。今日时间(字符串,表单提供者)方法 示例 1:
c sharp . c sharp . c sharp . c sharp
// 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 initializing 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);
}
}
Output: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: 为格式异常
c sharp . c sharp . c sharp . c sharp
// 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 initializing 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);
}
}
Output: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
参考:
版权属于:月萌API www.moonapi.com,转载请注明出处