C#日期时间格式 [英] c# datetime format
本文介绍了C#日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的DateTime可以采用特定的格式,具体取决于当前的文化背景.
I wish to have a specific format for my DateTime depending on the current culture.
所以我试试这个:
dateTime.ToString("dd/MM/yyyy hh:mm");
这在某种程度上是可以的,/替换为特定于文化的分隔符.但是,根据文化的不同,日期和月份的顺序不会切换(例如MM/dd).
This is partially OK, the / gets replaced by the culture-specific separator. But the day and month order are not switched (like MM/dd) depending on the culture.
使用 .ToString("g")
可以,但是其中不包括前导零.
Using .ToString("g")
works, but that doesn't include the leading zero.
我如何做到这一点?
推荐答案
我认为这可以满足您的要求:
I think this will do what you want:
System.Globalization.DateTimeFormatInfo format =
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
string dateTime = DateTime.Now.ToString(format.FullDateTimePattern);
您可以在较短的日期/时间内执行以下操作:
You can do the following for a short date/time:
System.Globalization.DateTimeFormatInfo format =
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
string dateTime = DateTime.Now.ToString(format.ShortDatePattern + " " +
format.ShortTimePattern);
这篇关于C#日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文