Need Help: DateTime.Parse CultureInfo Problem

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

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top