获得一天的后缀使用则DateTime.ToString时,() [英] Getting day suffix when using DateTime.ToString()
问题描述
是否有可能包括格式化使用则DateTime.ToString()?日期的当天后缀
Is it possible to include the day suffix when formatting a date using DateTime.ToString()?
例如我想打印以下格式的日期 - 周一7月27日2009年。但2009年7月的最近的例子我能找到使用则DateTime.ToString()是周一27
For example I would like to print the date in the following format - Monday 27th July 2009. However the closest example I can find using DateTime.ToString() is Monday 27 July 2009.
我能做到这一点有则DateTime.ToString()还是我将不得不回落到自己的code?
Can I do this with DateTime.ToString() or am I going to have to fall back to my own code?
推荐答案
作为参考,我总是用/引用的 SteveX字符串格式化 有没有出现在任何可用的变量中的任何日,但你可以轻松地构建一个字符串
As a reference I always use/refer to SteveX String Formatting and there doesn't appear to be any "th" in any of the available variables but you could easily build a string with
string.Format("{0:dddd dd}{1} {0:MMMM yyyy}", DateTime.Now, (?));
您将不得不提供一个ST1,次2,RD为3,而对于所有其他日,并可能是内衬用:言
You would then have to supply a "st" for 1, "nd" for 2, "rd" for 3, and "th" for all others and could be in-lined with a "? :" statement.
(DateTime.Now.Day % 10 == 1 && DateTime.Now.Day != 11) ? "st"
: (DateTime.Now.Day % 10 == 2 && DateTime.Now.Day != 12) ? "nd"
: (DateTime.Now.Day % 10 == 3 && DateTime.Now.Day != 13) ? "rd"
: "th"
这篇关于获得一天的后缀使用则DateTime.ToString时,()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!