包含日期/月/年,而不是日期的C#datetime格式 [英] C# datetime format that includes date/month/year and not day
问题描述
我想使用在PC的标准区域设置中显示日期,月份和年份的标准日期格式。但是,我只能找到列出日期和日期 - 月 - 年的D。有没有什么办法可以删除一天或其他任何方式来获得所需的输出?
I want to use a standard date format that displays date, month and year in the standard regional settings of the pc. However I could only find "D" which lists day along with Date-Month-Year. Is there any way I can remove the day from it or any other way to get my desired output?
DateTime date1 = new DateTime(2008, 4, 10);
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008
注意:我不想使用自定义格式(d MMMM yyyy),因为我想要维护订单的区域设置。
Note: I don't want to be using custom format (d MMMM yyyy) as I want the regional settings of the order to be maintained.
推荐答案
重新使用其中一种标准格式,那么显示的内容将取决于文化 - 所以即使你能找到一些不显示在美国的星期几,它仍然可以显示在其他文化
If you're using one of the standard formats, then what gets displayed is going to depend on the culture anyway - so even if you could find something which doesn't display the day of week in en-US, it may still display it in other cultures.
我想你可以为文化找到 DateTimeFormatInfo
,找到它的 LongDatePattern
,然后从该格式字符串中删除单个d的任何出现。这将是非常讨厌的。
I suppose you could find the DateTimeFormatInfo
for the culture, find its LongDatePattern
and then remove any occurrence of a single "d" from that format string. It would be pretty nasty though.
这篇关于包含日期/月/年,而不是日期的C#datetime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!