我知道我可以使用格式字符串,但我不想丢失日期/时间格式的文化特定表示。例如
2011 年 5 月 4 日 |下午 2:06 | ...
应该是 05/04/2011 |下午02:06 | ...
但是当我将其更改为不同的文化时,我希望它是
04.05.2011 | 14:06 | ...
不改变格式字符串。这可能吗?
最佳答案
您可以使用 DateTimeFormatInfo.CurrentInfo
ShortDatePattern
和 ShortTimePattern
做翻译:
// Change the culture to something different
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-AT");
DateTime test_datetime = new DateTime(2008, 10, 2, 3, 22, 12);
string s_date = test_datetime.ToString(DateTimeFormatInfo.CurrentInfo);
// For specifically the short date and time
string s_date1 =
test_datetime.ToString(DateTimeFormatInfo.CurrentInfo.ShortDatePattern);
string s_time1 =
test_datetime.ToString(DateTimeFormatInfo.CurrentInfo.ShortTimePattern);
// Results
// s_date1 == 02.10.2008
// s_time1 == 03:22
关于c# - 是否有一种文化安全的方法来获取带前导零的 ToShortDateString() 和 ToShortTimeString()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6654695/