0

The current regional settings on my Windows XP machine are English (United Kingdom). Using my C# app, how do I convert a DateTime object having value of 31/05/2012 to a US DateTime object ie. 05/31/2012? Is this possible without setting Thread.CurrentThread.CurrentCulture?

Thanks.

4

4 に答える 4

7

DateTime values don't have a culture - they don't know anything about formatting themselves. You specify culture information when you parse and format them. It's not clear where you're getting your value from, but to format it with the US culture, you'd just use something like:

CultureInfo us = CultureInfo.GetCultureInfo("en-US");
string text = dateTime.ToString("d", us); // d=short date format

Note that you should only perform this formatting when you have to display the information to the user - keep the value as a DateTime for as long as you can. For example, if you need to insert it into a database, use it as a parameter value in a parameterized SQL statement rather than including a text representation within the SQL itself.

于 2012-07-16T15:54:18.083 に答える
1
DateTime ukDateTimeFormat = DateTime.Parse("10/26/2009 06:47",      
System.Globalization.CultureInfo.GetCultureInfo("en-us"));

From UK format to US format, use following code snippet

DateTime usDateTimeFormat = DateTime.Parse("26/10/2009 06:47", 
System.Globalization.CultureInfo.GetCultureInfo("en-gb"));
于 2012-07-16T15:55:49.330 に答える
0

Use cultures.

See here - http://msdn.microsoft.com/en-us/library/5hh873ya.aspx

于 2012-07-16T15:51:57.057 に答える
0
DateTime dt = DateTime.Now;
string dtStringInUSFormat=dt.ToString(CultureInfo.GetCultureInfo("en-US"))

You need to import System.Globalization to access the CultureInfo class.

于 2012-07-16T15:53:42.313 に答える