Need Help: DateTime.Parse CultureInfo Problem

  • Thread starter Thread starter kilaen
  • Start date Start date
K

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;
}
 
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?
 
Back
Top