0

I need to convert DateTime variables between random cultures which will be read from config files.

I have the following code which works but I don't know why, and I'm not sure I trust it across all cases because the DateTime.Parse has no parameter specifying what format the convertedDt variable is in.

void ATestConversion(DateTime testDate) 
{
    CultureInfo ci = CultureInfo.CreateSpecificCulture("de-DE"); // assume de-DE read from config

    string convertedDt = testDate.ToString(ci);

    // How does this work without knowing convertedDt culture?
    DateTime myDate = DateTime.Parse(convertedDt, CultureInfo.CreateSpecificCulture("fr-FR")); // fr-FR read from config
}

However, whenever I try something like this code below that I would expect to work I get: string not recognized as a valid DateTime

myDate = DateTime.ParseExact(convertedDt, ci.DateTimeFormat.FullDateTimePattern, new CultureInfo("fr-FR"));

In this code I can provide the dynamic format that the inbound string is in, along with the CultureInfo of what it needs to be converted to.

I see many examples like this:

DateTime.ParseExact(myDate, "dd/MMM/yyyy HH:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture); 

where the format is hardcoded. I am basically trying to do the same but with dynamic formats. Why does my code not work when specifying the FullDateTimePattern of culture A and the actual CultureInfo of culture B?

How can I guarantee the conversions are correct between two run-time specified cultures?

wesmantooth
  • 625
  • 2
  • 11
  • 29
  • 3
    I'm not sure what your goal is - but `DateTime` _has no format or culture_. It's just a point in time, so converting to string using one culture or format then parsing it using another makes no sense. – D Stanley Jun 22 '16 at 17:02
  • 1
    The first example works because either the two formats are compatible or the date/time value has ambiguous month/day values (e.g. is `1-2-2016` January 2nd or February 1st?) – D Stanley Jun 22 '16 at 17:04
  • Here is the output from first example: `convertedDt = 31.12.1969 23:45:00` and `myDate = {12/31/1969 12:00:00 AM}` – wesmantooth Jun 22 '16 at 17:08
  • 1
    A `DateTime` variable in culture A has the exact same value in Culture B. Only how they are displayed changes. So, you could save as `InvariantCulture` to the "config file" and read it back as such without error. – Ňɏssa Pøngjǣrdenlarp Jun 22 '16 at 17:12
  • I see what you mean about DateTime being without format and culture. I will edit my question to convert to a different strings. The issue still remains that the DateTime.ParseExact does not work and I believe that is what I need in order to convert from date string in culture A to a date string in culture B – wesmantooth Jun 22 '16 at 17:18
  • 1
    The bottom line is if you want to parse a `DateTime` successfully you need to know the format it is in - otherwise you could get parse errors and/or invalid results. – D Stanley Jun 22 '16 at 17:26
  • 1
    ...that means saving meta data to describe the saved date string format, or do what the cool kids do and save everything to InvariantCulture format – Ňɏssa Pøngjǣrdenlarp Jun 22 '16 at 17:28

0 Answers0