.net Date.TryParseExact-提供程序参数用于什么?

.net Date.TryParseExact-提供程序参数用于什么?,.net,asp.net,parsing,.net,Asp.net,Parsing,Date.TryParse的方法签名为: Public Shared Function TryParseExact ( _ s As String, _ format As String, _ provider As IFormatProvider, _ style As DateTimeStyles, _ <OutAttribute> ByRef result As DateTime _ ) As Boolean 公共共享函数TryPars

Date.TryParse的方法签名为:

Public Shared Function TryParseExact ( _
    s As String, _
    format As String, _
    provider As IFormatProvider, _
    style As DateTimeStyles, _
    <OutAttribute> ByRef result As DateTime _
) As Boolean
公共共享函数TryParseExact(_
它就像一根绳子_
格式为字符串_
供应商作为供应商_
样式为DateTimeStyles_
ByRef结果作为日期时间_
)作为布尔值
我了解格式的作用,但provider参数是什么?我知道您传入了一个CultureInfo实例,但我不确定它的用途


有人能告诉我吗?

提供商指定了有关日期的特定于区域性的格式信息

例如,您可以传入以下任何一种文化:

new CultureInfo("en-US")  // USA
new CultureInfo("fr-FR")  // France
new CultureInfo("it-IT")  // Italy
new CultureInfo("de-DE")  // Germany
您可以根据这些文化设置日期格式,例如:

  • 美国英语:2009年1月6日下午4:37:00
  • fr:01/06/2009 16:37:00
  • it:01/06/2009 16.37.00
  • de:01.06.2009 16:37:00
另一个示例:在使用en-US for CultureInfo时,使用代表M/d/yyyy短日期模式的“d”格式,请考虑:

DateTime dateValue;
string[] sampleDates = new[] { "31/8/2009", "8/31/2009" };
foreach (string currentDate in sampleDates)
{
    bool result = DateTime.TryParseExact(currentDate, new[] {"d"}, 
                    new CultureInfo("en-US"), 
                    DateTimeStyles.None, 
                    out dateValue);
    Console.WriteLine("{0}: {1}", currentDate, result ? "valid" : "invalid");
    if (result)
    {
        Console.WriteLine("Result: {0}", dateValue);
    }
    Console.WriteLine();
}
输出:

31/8/2009: invalid

8/31/2009: valid
Result: 8/31/2009 12:00:00 AM

2009年8月31日无效,因为它不符合美国文化格式M/d/yyyy,而2009年8月31日有效,因为它符合美国文化格式M/d/yyyy。

提供程序指定有关日期的文化特定格式信息

例如,您可以传入以下任何一种文化:

new CultureInfo("en-US")  // USA
new CultureInfo("fr-FR")  // France
new CultureInfo("it-IT")  // Italy
new CultureInfo("de-DE")  // Germany
您可以根据这些文化设置日期格式,例如:

  • 美国英语:2009年1月6日下午4:37:00
  • fr:01/06/2009 16:37:00
  • it:01/06/2009 16.37.00
  • de:01.06.2009 16:37:00
另一个示例:在使用en-US for CultureInfo时,使用代表M/d/yyyy短日期模式的“d”格式,请考虑:

DateTime dateValue;
string[] sampleDates = new[] { "31/8/2009", "8/31/2009" };
foreach (string currentDate in sampleDates)
{
    bool result = DateTime.TryParseExact(currentDate, new[] {"d"}, 
                    new CultureInfo("en-US"), 
                    DateTimeStyles.None, 
                    out dateValue);
    Console.WriteLine("{0}: {1}", currentDate, result ? "valid" : "invalid");
    if (result)
    {
        Console.WriteLine("Result: {0}", dateValue);
    }
    Console.WriteLine();
}
输出:

31/8/2009: invalid

8/31/2009: valid
Result: 8/31/2009 12:00:00 AM

2009年8月31日无效,因为它不符合M/d/yyyy的英美文化格式,而2009年8月31日有效,因为它符合。

IFormatProvider是一个了解如何格式化内容的类
TryParseExact
需要询问它(在本例中是一个
CultureInfo
),以查看字符串中是否有任何特定于区域性的特殊符号,如星期几、上午/下午等


此外,如果您只是将格式作为标准的c#date格式说明符之一传入,则必须咨询格式提供程序,以了解它对于给定区域性的确切含义。

a
IFormatProvider
是一个了解如何格式化内容的类
TryParseExact
需要询问它(在本例中是一个
CultureInfo
),以查看字符串中是否有任何特定于区域性的特殊符号,如星期几、上午/下午等


此外,如果您只是将格式作为标准的c#date格式说明符之一传入,则必须咨询格式提供程序,以了解该格式对于给定文化的确切含义。

世界各地的日期都不同,无论是在格式上还是在几个月内使用的词语上。IFormatProviders知道这一点。

世界各地的日期各不相同,无论是在格式上还是在几个月内使用的词语上。供应商知道这一点。

我知道了。我的印象是,IFormatProvider参数是一种“后备”格式匹配器,以防传入的格式字符串无法完成此任务。假设我尝试这样做:TryParseExact(“18/06/2009”,“ddMMyyyy”,New CultureInfo(“en-AU”,DateTimeStyles.None,stringToWriteTo),那么TryParseExact将返回false,并且不会向stringToWriteTo写入任何内容?我明白了。我的印象是,IFormatProvider参数是一种“回退”格式匹配器如果传入的格式字符串不起作用。假设我尝试执行:TryParseExact(“18/06/2009”,“ddMMyyyy”,New CultureInfo(“en-AU”,DateTimeStyles.None,stringToWriteTo”),那么TryParseExact将返回false,并且不会向stringToWriteTo写入任何内容?