使用DateTime.ToString()获取日期后缀 [英] Getting day suffix when using DateTime.ToString()

查看:103
本文介绍了使用DateTime.ToString()获取日期后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用DateTime.ToString()格式化日期可以包括日期后缀?

Is it possible to include the day suffix when formatting a date using DateTime.ToString()?

例如,我想以以下格式打印日期 - 2009年7月27日,星期一。然而,使用DateTime.ToString()可以找到最接近的例子是2009年7月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()或者我将不得不回到自己的代码?

Can I do this with DateTime.ToString() or am I going to have to fall back to my own code?

推荐答案

作为参考,我总是使用/引用 SteveX字符串格式
,似乎没有任何th 在任何可用的变量中,但您可以轻松地使用

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, (?));

然后你必须提供一个st为1,nd为2,rd 3,th为所有其他人,并且可以内置?语句。

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屋!

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