59

If we're using the ParseExact method for exact date-time's parsing using a specified format, why do we need to provide a IFormatProvider object? what is the point behind it?

For example:

DateTime.ParseExact(dateString, format, provider);

Why do we need the provider here?

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108

1 Answers1

72

why do we need to provide a IFormatProvider object? what is the point behind it?

It allows for culture-specific options. In particular:

  • The format you use could be a standard date/time format, which means different patterns in different cultures
  • You could use : or / in your pattern, which mean culture-specific characters for the time separator or date separator respectively
  • When parsing month and day names, those clearly depend on culture
  • The culture determines the default calendar as well, which will affect the result

As an example of the last point, consider the same exact string and format, interpreted in the culture of the US or Saudi Arabia:

using System;
using System.Globalization;

class Test
{
    static void Main()        
    {
        CultureInfo us = new CultureInfo("en-US");
        CultureInfo sa = new CultureInfo("ar-SA");
        string text = "1434-09-23T15:16";
        string format = "yyyy'-'MM'-'dd'T'HH':'mm";
        Console.WriteLine(DateTime.ParseExact(text, format, us));
        Console.WriteLine(DateTime.ParseExact(text, format, sa));
    }
} 

When parsing with the US culture, the Gregorian calendar is used - whereas when parsing with the Saudi Arabian culture, the Um Al Qura calendar is used, where 1434 is the year we're currently in (as I write this answer).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    The calendar system change is surprising. I wasn't aware of that. Do you know of any resource that lists which BCL calendars are tied to which culture settings? – Matt Johnson-Pint Sep 23 '13 at 14:33
  • 1
    Jon, can you please explain what does the 'T' means in your date-time text (`1434-09-23T15:16`) example? – Yair Nevet Sep 23 '13 at 19:00
  • @YairNevet: It's just to separate the date from the time, following ISO-8601 format. – Jon Skeet Sep 23 '13 at 19:49
  • @JonSkeet: i'm doing this,`CultureInfo us = new CultureInfo("en-US"); string format = "yyyy'-'MM'-'dd'T'HH':'mm"; DateTime d = DateTime.ParseExact(DateTime.Now.ToString(), format, us);` but getting error `String was not recognized as a valid DateTime.` – RobertKing Feb 05 '14 at 12:38
  • @Rohaan: Well yes, because presumably `DateTime.Now.ToString()` isn't returning a string in that format... – Jon Skeet Feb 05 '14 at 12:42
  • @JonSkeet: so in which format we need to pass the date string – RobertKing Feb 05 '14 at 12:59
  • @Rohaan: In the format you're specifying - but it's unclear what you're even trying to do in your example, as you're *starting* with a `DateTime`... why are you converting it to a string and then parsing it? I suggest you create a new question with more details. – Jon Skeet Feb 05 '14 at 13:00
  • @JonSkeet: please see edited question @ link http://stackoverflow.com/questions/21574549/how-to-convert-string-to-a-datetime-in-c – RobertKing Feb 05 '14 at 13:16
  • @JonSkeet, what are single quotation marks for? I've not been using them for a looong time with no problems. – anar khalilov Apr 17 '15 at 08:08
  • @AnarKhalilov: They're escaping, basically - to say that what's inside the quotes should be handled literally. While they're not strictly necessary here, they make it clear that you're *not* trying to use a culture-specific symbol, and it makes it easier to change to use `/` always (rather than the culture-specific date separator) later, if you want. – Jon Skeet Apr 17 '15 at 08:15
  • 5
    @JonSkeet So basically, if you have a string with custom format "y2015m04d17" and use a parse format of "'y'yyyy'm'MM'd'dd", you can tell the parser to leave 'y', m' and 'd' alone, and not to interpret them as year/month/day. Otherwise the parser might feel confused, right? :) – anar khalilov Apr 17 '15 at 08:38
  • @AnarKhalilov: Yes, exactly. – Jon Skeet Apr 17 '15 at 08:42