Reputation: 2483
Is it possible to force a DateTime object to use a different locale? I wish to populate a DateTime object with a UK DateTime but formatted as US.
I have tried the following:
DateTime ukDateTimeFormat = DateTime.Parse("10/26/2009 06:47", CultureInfo.GetCultureInfo("en-us"));
DateTime usDateTimeFormat = DateTime.Parse("26/10/2009 06:47", CultureInfo.GetCultureInfo("en-gb"));
string strDate = DateTime.Now.ToString(CultureInfo.InvariantCulture);
string[] dateString = strDate.Split('/');
DateTime enterDate = DateTime.Parse(dateString[0] + "/" + dateString[1] + "/" + dateString[2], CultureInfo.GetCultureInfo("en-us"));
Nothing works, I always end up with a UK formatted date.
Any help would be much appreciated :-)
Upvotes: 2
Views: 313
Reputation: 1
To format your date for the locale, use this code:
string formattedDate = ukDateFormat.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-us"))
Upvotes: 0
Reputation: 22379
It seems like you're confused between representing a date-time and formatting a date-time.
DateTime
does not contain any format, it only represents the actual time. So the question about a US/UK format of a DateTime is meaningless.
If you want to display the time in a different format, that's not a DateTime
, that's a string
. You can use the various overloads of DateTime.ToString(...)
in order to achieve different formatting as a string. There are some built-in formats, and you can specify a locale.
Upvotes: 4
Reputation: 33857
The DateTime
object does not have an internal string format as such - your date is stored as a date and formatted on output. You can populate however you wish, however when outputting it, you'll need to specify your format, e.g.:
string formattedDate = ukDateFormat.ToString("MM/dd/yyyy HH:mm");
Upvotes: 3