简体   繁体   中英

Converting string to date in another culture

I am trying to convert a string (which represents date in invariantCulture) to dateTime in given culture. The problem is that when the date is converted to German culture, the day becomes month and month becomes day. What is wrong with below code or am i missing something ?

var day = 11; var month = 12; var year = 2014;

var someDate = new DateTime(year, month, day);
var theDay = someDate.Day;//11 ok as expected
var theMonth = someDate.Month; //12 ok as expected

var dateString = someDate.ToString(CultureInfo.InvariantCulture);

var date1 = DateTime.Parse(dateString, CultureInfo.GetCultureInfo("de-De"));
var day1 = date1.Day;//12 this should be 11 ?
var month1 = date1.Month; //11 this should be 12 ?

The second argument to DateTime.Parse is used to tell the parser what format the string is in, not what format you want to convert it to. You are generating an invariant string and then parsing it as a German string which is why your day and month are getting swapped.

If your goal is to get a German string representation of the date, just use var dateString = someDate.ToString(CultureInfo.GetCultureInfo("de-DE")) .

I guess de-De culture doesn't have a standard date and time format as MM/dd/yyyy HH:mm:ss .

Since you using DateTime.ToString() method with InvariantCulture , result string will be "G" standard format which is MM/dd/yyyy HH:mm:ss for InvariantCulture .

Because of that, dateString will be 12/11/2014 00:00:00 and de-DE culture doesn't have a standard date and time format MM/dd/yyyy HH:mm:ss but has dd/MM/yyyy HH:mm:ss which is dd.MM.yyyy HH:mm:ss for de-DE culture.

That's why DateTime.Parse method matches pattern which is dd/MM/yyyy HH:mm:ss (since it's DateSeparator is . it should be dd.MM.yyyy HH:mm:ss format).

That's why it parses your 12 as a Day and 11 as a Month .

If you already a DateTime (which you have) just use .ToString() method with your de-DE culture like;

var culture = new CultureInfo("de-De");
var dateString = someDate.ToString(culture);

Remember, a DateTime doesn't have any implicit format or culture. It just have date and time values. String representations of them can have formats.

By the way, you can find all standard date and time patterns your de-DE culture like;

var culture = new CultureInfo("de-De");
foreach (var format in culture.DateTimeFormat.GetAllDateTimePatterns())
{
    Console.WriteLine(format);
}

Change the following line and test it again:

 var dateString = someDate.ToString(CultureInfo.InvariantCulture);

to:

 var dateString = someDate.ToString("O");

or:

 var dateString = someDate.ToString("S");

ok, here is what i think what you want to accomplish, not sure if i got you right: you want to read an invariant cultured date string and convert it to a german cultured date string. but in your example you are trying to parse an invariant cultured date AS a german cultured date. of course that leads to a misinterpretation. try this:

string invariantCultureDateString = "12/11/2014 00:00:00";
var dateTime = DateTime.Parse(invariantCultureDateString, CultureInfo.InvariantCulture);
string germanCultureDateString = dateTime.ToString(CultureInfo.GetCultureInfo("de-De"));

BR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM