Home All Groups Group Topic Archive Search About

Need Help: DateTime.Parse CultureInfo Problem

Author
5 Apr 2007 7:46 PM
kilaen
I have this method that converts dates from a culture of origin to a
destination culture. It's not working as intended, hopefully someone
can enlighten me and tell me what I'm doing wrong. I've included the
code, the values being passed in, and the output. Thanks in advance!

value = "01/04/2007 12:30:48"
selected = {en-GB}
origin = {en-GB}
to = {en-US}
result = {1/4/2007 12:30:48 PM}

I expected to see: {4/1/2007 12:30:48PM}

public static DateTime ConvertDateTime(string value, CultureInfo
selected, CultureInfo origin, CultureInfo to)
{
    DateTime result;
    try
    {
        result = DateTime.Parse(value, to);
    }
    catch (Exception)
    {
        result = DateTime.Parse(value, selected);
    }
    return result;
}

Author
5 Apr 2007 11:19 PM
Göran_Andersson
kil***@gmail.com wrote:
> I have this method that converts dates from a culture of origin to a
> destination culture.

No, it does not. It parses the text representation of a date from one of
two cultures into a DateTime value. A DateTime value is culture neutral.

> It's not working as intended, hopefully someone
> can enlighten me and tell me what I'm doing wrong. I've included the
> code, the values being passed in, and the output. Thanks in advance!
>
> value = "01/04/2007 12:30:48"
> selected = {en-GB}
> origin = {en-GB}
> to = {en-US}
> result = {1/4/2007 12:30:48 PM}

You are parsing the date using the en-US culture. What culture have you
used to create the textual representation of the result?

Show quote
> I expected to see: {4/1/2007 12:30:48PM}
>
> public static DateTime ConvertDateTime(string value, CultureInfo
> selected, CultureInfo origin, CultureInfo to)
> {
>     DateTime result;
>     try
>     {
>         result = DateTime.Parse(value, to);
>     }
>     catch (Exception)
>     {
>         result = DateTime.Parse(value, selected);
>     }
>     return result;
> }
>


--
Göran Andersson
_____
http://www.guffa.com

AddThis Social Bookmark Button