Share via


How to convert date in English culture to a different culture

Question

Thursday, May 21, 2009 3:15 PM

Hello,

I have unit test that I have hard coded dates in the English culture; however, there are instances where I will need to format the date into a different culture, such as Spanish. Rather than rely on the developer to know how to format the culture, I would prefer to allow them to enter the date format in English and then have my code convert it; however, I am having problems with trying to figure out how to do so. Here is a sample of my problem:

string enDate = "01/13/1983";

CultureInfo cultureInfo = new CultureInfo("es-MX", true);
            
DateTime newDate = DateTime.Parse(enDate, cultureInfo);

Console.Write(newDate);

I am getting an exception thrown because DateTime.Parse doesn't like my date format for Spanish because my enDate is still stuck in English. Is it possible to take my enDate variable and convert it over?

Thanks,

Flea#

http://fleasharp.wordpress.com

All replies (9)

Thursday, May 21, 2009 3:19 PM âś…Answered | 1 vote

You have to parse in English, then output in Spanish.

string date = "01/13/1983";
DateTime dateTime = DateTime.Parse(date);
Console.WriteLine(dateTime.ToString(CultureInfo.GetCultureInfo("es-MX")));

You can also use ParseExact if you'd like:

string date = "01/13/1983";
DateTime dateTime = DateTime.ParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dateTime.ToString(CultureInfo.GetCultureInfo("es-MX")));


David Morton - http://blog.davemorton.net/ - @davidmmorton


Thursday, May 21, 2009 3:25 PM

Thanks David, that fixed it! I appreciate your assistance!

Flea

http://fleasharp.wordpress.com


Thursday, May 21, 2009 4:45 PM

Actually, that code makes the assumption that the test code is always executed on a US machine. To remove that assumption it would be:

string date = "01/13/1983"; // en-US formatted date
// Convert to DateTime
DateTime parsedDate = DateTime.Parse(date, CultureInfo.GetCultureInfo("en-US"));
// Output in es-MX format
Console.WriteLine(parsedDate.ToString(CultureInfo.GetCultureInfo("es-MX")));

Thursday, May 21, 2009 4:46 PM

Actually, that code makes the assumption that the test code is always executed on a US machine. To remove that assumption it would be:

The second block doesn't.David Morton - http://blog.davemorton.net/ - @davidmmorton


Thursday, May 21, 2009 4:54 PM

And I like the ParseExact usage better, it's more explicit. So I only take issue with the words, "can, if you like". That doesn't provide an impulse to do it "correctly", or educate the reader.

(I know, I'm a stickler for details)


Thursday, May 21, 2009 5:25 PM

I should have mentioned, but this project I am working on is using Visual Studio 2003 and I do not have access to the static method of GetCultureInfo.

Using what was provided, I was able to get a string version of the newly formatted date, but when I try and take that culture specific formatted string and try to parse it back to a datetime object; it reverts it back to en-US format.  Try running this code block inside just a simple console application you will see what I mean.  Why on Earth would it be reverting it back to US format?

CultureInfo currentCulture = new CultureInfo("es-MX", true);
string enDate = "01/13/1983";
DateTime dateToCheck;

// Parse the english date string into a date time object
dateToCheck = DateTime.Parse(enDate);
            
// Obtain the formatted string based on the culture passed in.
string formattedDate = dateToCheck.ToString(currentCulture);

// Assign the new formatted date object back to dateToCheck
dateToCheck = DateTime.Parse(formattedDate, currentCulture);

Console.Write(dateToCheck.ToString());

http://fleasharp.wordpress.com


Thursday, May 21, 2009 6:09 PM

DateTime is not stored in any culture specific format. It's stored in a machine format. It's only when you take input from a user (in this case the programmer) and parse it, or you want to output the date to a user (say in a text box, or to a stream like stdout) that you have to be concerned with culture.

Your Console.Write method is taking the culture-less date and converting it to a string using the thread's current culture setting, which on a U.S. machine will be in U.S. format by default (the zero-parameter overload of ToString uses the current culture). If you want to output it in another culture format, you have to pass an IFormatProvider to ToString (or any of the other 101 date to string / string to date conversion methods).


Thursday, May 21, 2009 6:12 PM

The GetCulture method can be replaced with the following method in .NET 1.1:

static CultureInfo GetCulture(string name)
{
    CultureInfo c = null;
    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
    for (int i = 0; i < cultures.Length; i++)
    {
        if (cultures[i].Name == "es-MX")
        {
            c = cultures[i];
            break;
        }
    }
    return c;
}

So pass "es-MX" into that method, and you should get the right culture.  Then simply take the returned value, and pass it into the ToString method of the dateToCheck when outputting.David Morton - http://blog.davemorton.net/ - @davidmmorton


Thursday, May 21, 2009 7:22 PM

Tergiver, thanks for the explanation, I really appreciate it. That clears up a lot!

David thanks for the additional code sample!

-Flea