What does IFormatProvider do?

后端 未结 8 2192
一向
一向 2021-01-30 12:27

I was playing around with the Datetime.ParseExact method, and it wants an IFormatProvider...

It works inputting null, but what exactly does it do?

8条回答
  •  伪装坚强ぢ
    2021-01-30 12:45

    Passing null as the IFormatProvider is not the correct way to do this. If the user has a custom date/time format on their PC you'll have issues in parsing and converting to string. I've just fixed a bug where somebody had passed null as the IFormatProvider when converting to string.

    Instead you should be using CultureInfo.InvariantCulture

    Wrong:

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

    Correct:

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

提交回复
热议问题