How to create a DateTime object not in the current culture

I really draw a space on this. I'm working on globalization, but DateTime seems to always return to the CurrentThread culture. I broke it into smaller steps, hoping to find my problem, but it starts to drive me crazy.

I have a text box with a date expressed as a string:

// the CurrentThread culture is de-DE // My test browser is also set to de-DE IFormatProvider culture = new System.Globalization.CultureInfo("de-DE", true); // en-US culture, what I'd ultimately like to see the DateTime in IFormatProvider us_culture = new System.Globalization.CultureInfo("en-US", true); // correctly reads the textbox value (22.7.2010 into a datetime) DateTime dt= DateTime.Parse(txtStartDate.Text, culture, System.Globalization.DateTimeStyles.NoCurrentDateDefault); // correctly produces a string 7/22/2010 string dt2 = dt.ToString(us_culture); 

At this point, I want a DateTime, which in en-US I tried both:

  DateTime dt3 = Convert.ToDateTime(dt2, us_culture); DateTime dt3 = DateTime.Parse(dt2, us_culture); 

But both produce de-DE DateTimes. The motivation for asking this question is that the rest of the business logic will call dt2.toString () and result in an invalid date time string. I understand that I can change toString () as toString (us_culture), but I would prefer not to change the rest of the business logic to accommodate this change.

Is there a way to get a DateTime in a culture other than the CurrentThread culture?

Thanks for your time, I've been banging my head about this for too long.

+4
source share
3 answers

A DateTime object is just that - a date / time, it is an agnostic culture. You will need to format the dates using a specific culture, if you need it.

+4
source

The less overload of ToString () parameter always displays the date according to the CurrentCulture setting. This is what you want 99 times 100.

If you do not want to use overloading, you can manually set CurrentThread.CurrentCulture to what you want.

 System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE"); 

But understand that this will change for everything in your application.

+2
source

try using the user data format:

 string dt2 = dt.ToString("m/d/yyyy"); 
+1
source

All Articles