[英]What's the purpose of the format provider in ParseExact() of DateTime and DateTimeOffset?
Let us consider DateTimeOffset.ParseExact让我们考虑DateTimeOffset.ParseExact
public static DateTime ParseExact (
string s,
string format,
IFormatProvider? provider,
System.Globalization.DateTimeStyles style
);
The expected format is already described via the format
parameter, so what's the purpose of the provider
parameter?预期的格式已经通过
format
参数进行了描述,那么provider
参数的目的是什么? Can anyone please give an example how different provider
values can lead to different results?谁能举例说明不同的
provider
值如何导致不同的结果?
The meaning of various elements of a custom datetime format string depend on the format provider.自定义日期时间格式字符串的各种元素的含义取决于格式提供程序。
Example:例子:
var aString = "12 avr. 2021";
var gbCulture = new CultureInfo("en-GB");
var frCulture = new CultureInfo("fr-FR");
var canParseGb =
DateTime.TryParseExact(aString, "dd MMM yyyy", gbCulture, DateTimeStyles.None, out var gbDateTime);
var canParseFr =
DateTime.TryParseExact(aString, "dd MMM yyyy", frCulture, DateTimeStyles.None, out var frDateTime);
Same input string s
, same format string format
, different provider
s => different results.相同的输入字符串
s
,相同的格式字符串format
,不同的provider
s => 不同的结果。
For example CultureInfo
implements IFormatProvider
.例如
CultureInfo
实现IFormatProvider
。 So you can pass a culture which has it's own formatting rules apart from your provided format string.因此,除了您提供的格式字符串之外,您还可以传递具有自己的格式规则的文化。
Also note that the format string can contain specifiers that will behave differently with different cultures/format-providers like the "/" custom format specifier :另请注意,格式字符串可以包含说明符,这些说明符在不同的文化/格式提供者中表现不同,例如“/”自定义格式说明符:
CultureInfo deCulture = new CultureInfo("de-DE"); // german
CultureInfo frCulture = new CultureInfo("fr-FR"); // french
// works because / is replaced with the passed culture's date-separator which is . in germany
DateTime dateDe = DateTime.ParseExact("25.02.2021", "dd/MM/yyyy", deCulture, DateTimeStyles.None);
// works not since french use / as date-separator
DateTime dateFr = DateTime.ParseExact("25.02.2021", "dd/MM/yyyy", frCulture, DateTimeStyles.None);
As Magnetron has commented the same applies to the ":" custom time format specifier正如磁控管所评论的,这同样适用于“:”自定义时间格式说明符
Different cultures will use different values for things such as month names and days of the week.不同的文化会使用不同的值来表示月份名称和星期几。
Jul
might be July to you, but in France it's not. Jul
对你来说可能是七月,但在法国却不是。 For example:例如:
var frenchDate = DateTimeOffset.ParseExact("1 févr. 2020", "d MMM yyyy",
CultureInfo.GetCultureInfo("fr-FR"));
var englishDate = DateTimeOffset.ParseExact("1 Feb 2020", "d MMM yyyy",
CultureInfo.GetCultureInfo("en-GB"));
All of these details can be seen in the CultureInfo
object, for example fr-FR
looks like this:所有这些细节都可以在
CultureInfo
object 中看到,例如fr-FR
看起来像这样:
The documentation is quite clear on this:文档对此非常清楚:
The particular date and time symbols and strings used in
input
are defined by theformatProvider
parameter...input
中使用的特定日期和时间符号和字符串由formatProvider
参数定义...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.