简体   繁体   English

如何在DateTime.ParseExact中使用IFormatProvider

[英]How to use IFormatProvider in DateTime.ParseExact

如果已经有格式参数,我们为什么要在DateTime.ParseExact使用IFormatProvider

DateTime.ParseExact(inputString, format, cultureInfo);

The format parameter says what pattern to use - but it doesn't say anything about which calendar, month names, short date format etc to use. format参数说明要使用的模式 - 但它没有说明要使用哪个日历,月份名称,短日期格式等。 That's up to the IFormatProvider . 这取决于IFormatProvider

For example, suppose you wanted to parse a value with the pattern "dd MMMM yyyy" - which month names would you expect to work? 例如,假设您想要解析具有"dd MMMM yyyy"模式的值 - 您希望哪个月份名称可以使用? If you're using a month name of "February" but you're running on a machine with a system culture of French, it would fail - you'd need to specify an English culture (or the invariant culture) to get it to work. 如果您使用的是月份名称“二月”,但是您运行的是具有法语系统文化的计算机,那么它将失败 - 您需要指定英语文化(或不变文化)才能使其成为工作。 Likewise, you could specify a pattern of "d" to mean the short date format - but which short date format? 同样,你可以指定一个"d"模式来表示短日期格式 - 但是哪个短日期格式?

Even the calendar you use is affected by the format provider: the value could be parsed into the same year, month and day values in two cultures - but the meaning of those values would be very different in a Hijri calendar from a Gregorian calendar, for example. 即使您使用的日历也受格式提供者的影响:可以在两种文化中将值解析为相同的年,月和日值 - 但这些值的含义在公历中与公历中的差异非常大,例。

A simple example: / 一个简单的例子: /

/ is not just a char, but a date separator that depends on the culture. /不仅仅是一个char,而是一个依赖于文化的日期分隔符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM