C#日期时间短与国家公约 [英] C# Datetime short year with national conventions
问题描述
如何使DateTime.ToString()以这种方式输出字符串,在德国看起来像这样:dd.MM.yy,在美国MM / dd / yy和其他地方也是这样,一年总是有两个字符,但日期的格式,具体到文化。
你只需要使用 DateTime.ToString
,因为它使用当前的文化,如果你不指定一个不同的:
string result = dt.ToString(MM / dd / yy);
请参阅:/自定义格式说明符
编辑: dd MM
和 MM dd
部分怎么样?
好的问题,这是我的尝试:
字符串shortDate = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
string result = dt.ToString(shortDate.Replace(yyyy,yy));
How can I make DateTime.ToString() put out strings in such a way, that it looks like this in Germany: dd.MM.yy and like this in the USA MM/dd/yy and everywhere else for that matter, with year always having two characters, but the format of the date, being specific to culture.
You just have to use DateTime.ToString
since it's using the current culture if you don't specify a different:
string result = dt.ToString("MM/dd/yy");
The /
will be replaced with the correct date separators of the current culture.
See: The "/" Custom Format Specifier
Edit: "what about dd MM
and MM dd
part?"
Good question, this is my try:
string shortDate = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
string result = dt.ToString(shortDate.Replace("yyyy", "yy"));
这篇关于C#日期时间短与国家公约的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!