我怎样才能让 DateTime.ToString() 以这样的方式输出字符串,它在德国看起来像这样:dd.MM.yy,在美国 MM/dd/yy 和其他任何地方都像这样, 年份总是有两个字符,但日期的格式特定于文化。
最佳答案
您只需要使用 DateTime.ToString
,因为如果您不指定不同的文化,它会使用当前的文化:
string result = dt.ToString("MM/dd/yy");
/
将替换为当前区域性的正确日期分隔符。
参见:The "/" Custom Format Specifier
编辑:“dd MM
和MM dd
部分呢?”
好问题,这是我的尝试:
string shortDate = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
string result = dt.ToString(shortDate.Replace("yyyy", "yy"));
关于C# Datetime 短年份与国家公约,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19950470/