C#日期时间短与国家公约 [英] C# Datetime short year with national conventions

查看:184
本文介绍了C#日期时间短与国家公约的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆