What does IFormatProvider do?

I played with the Datetime.ParseExact method and it wants IFormatProvider ...

It does null input, but what exactly does it do?

+64
c # iformatprovider
Feb 03 '09 at 10:46
source share
8 answers

CultureInfo also implements this interface and can be used in your case. So you can parse a French date string, for example; you can use

var ci = new CultureInfo("fr-FR"); DateTime dt = DateTime.Parse(yourDateInputString, yourFormatString, ci); 
+41
Feb 04 '09 at 13:13
source share

The IFormatProvider interface IFormatProvider usually implemented for you by the CultureInfo class, for example:

  • CultureInfo.CurrentCulture
  • CultureInfo.CurrentUICulture
  • CultureInfo.InvariantCulture
  • CultureInfo.CreateSpecificCulture("de-CA") //German (Canada)

The interface is the gateway to a function that allows you to retrieve a set of culture-related data from a culture. The two public cultural objects that IFormatProvider can request are:

  • DateTimeFormatInfo
  • NumberFormatInfo

As this usually works, you request an IFormatProvider to give you a DateTimeFormatInfo object:

 DateTimeFormatInfo format; format = (DateTimeFormatInfo)provider.GetFormat(typeof(DateTimeFormatInfo)); if (format != null) DoStuffWithDatesOrTimes(format); 

Itโ€™s also known IFormatProvider that any IFormatProvider interface is probably implemented by a class that descends from CultureInfo or descends from DateTimeFormatInfo , so you can directly use the interface:

 CultureInfo info = provider as CultureInfo; if (info != null) format = info.DateTimeInfo; else { DateTimeFormatInfo dtfi = provider as DateTimeFormatInfo; if (dtfi != null) format = dtfi; else format = (DateTimeFormatInfo)provider.GetFormat(typeof(DateTimeFormatInfo)); } if (format != null) DoStuffWithDatesOrTimes(format); 

But don't do it

All this hard work has already been written for you:

To get DateTimeFormatInfo from IFormatProvider :

 DateTimeFormatInfo format = DateTimeFormatInfo.GetInstance(provider); 

To get NumberFormatInfo from IFormatProvider :

 NumberFormatInfo format = NumberFormatInfo.GetInstance(provider); 

The value of IFormatProvider is that you create your own cultural objects. As long as they implement IFormatProvider and return the objects they are asked for, you can bypass built-in cultures.

You can also use IFormatProvider for a method of transferring arbitrary cultural objects - through IFormatProvider . For example. name of god in different cultures

  • the God
  • the God
  • Jehova
  • Yahweh
  • ื™ื”ื•ื”
  • ืื”ื™ื” ืืฉืจ ืื”ื™ื”

This allows your custom LordsNameFormatInfo class LordsNameFormatInfo navigate inside the IFormatProvider , and you can save idioms.

In fact, you will never need to call GetFormat method yourself.

Whenever you need an IFormatProvider , you can pass a CultureInfo object:

 DateTime.Now.ToString(CultureInfo.CurrentCulture); endTime.ToString(CultureInfo.InvariantCulture); transactionID.toString(CultureInfo.CreateSpecificCulture("qps-ploc")); 

Note Any code issued in a public domain. No attribution required.

+27
Aug 12 '13 at 19:41
source share

Passing a null value as an IFormatProvider not the correct way. If the user has his own date and time format on his PC, you will have problems parsing and converting to a string. I just fixed the error when someone passed null as an IFormatProvider when converting to a string.

Instead, you should use CultureInfo.InvariantCulture

Wrong:

 string output = theDate.ToString("dd/MM/yy HH:mm:ss.fff", null); 

Right:

 string output = theDate.ToString("dd/MM/yy HH:mm:ss.fff", CultureInfo.InvariantCulture); 
+14
Jan 07 '13 at 11:26
source share

Here you can see http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

See the comments and examples section.

+5
Feb 03 '09 at 10:49
source share

IFormatProvider provides culture information to this method. DateTimeFormatInfo implements IFormatProvider and allows you to specify the format in which you want to display the date / time. Examples can be found in the respective MSDN Pages.

+5
Feb 03 '09 at 11:09
source share
+2
03 Feb '09 at 10:49
source share

MSDN

The .NET Framework includes the following predefined IFormatProvider implementations of three to provide culture-related information that is used to format or parse numbers and date and time values:

  • The NumberFormatInfo class, which provides information that is used to format numbers, such as currency separator characters, thousand separators, and decimal separators for a specific culture. For information about predefined format strings that are recognized by the NumberFormatInfo object and used in numeric formatting operations, see Standard NumberFormatInfo and NumberFormatInfo Format Strings.
  • A DateTimeFormatInfo class that provides information that is used to format dates and times, such as date and time separator characters for a specific culture or the order and format of date, month, and day components. For information about predefined format strings recognized by the DateTimeFormatInfo object and used in numeric formatting, see Standard Date and Time Strings and Custom Date and Time Strings.
  • The CultureInfo class representing a specific culture. Its GetFormat method returns a culture- NumberFormatInfo or DateTimeFormatInfo object, depending on whether the CultureInfo object is used in a formatting or parsing operation that includes numbers or dates and times.

The .NET Framework also supports custom formatting. This is usually due to the creation of a formatting class that implements both IFormatProvider and ICustomFormatter. Then an instance of this class is passed as a parameter to a method that performs a custom formatting operation, for example String.Format(IFormatProvider, String, Object[]) .

+2
Feb 02 '17 at 3:05
source share

The DateTimeFormatInfo class implements this interface, so it allows you to control the formatting of DateTime strings.

+1
Feb 03 '09 at 10:50
source share



All Articles