我怎样才能得到下面提到的 c# 中的日期格式。
对于 2010 年 11 月 1 日,应显示为:11 月 1 日
对于 2010 年 11 月 30 日,应显示为:11 月 30 日
我们可以使用任何日期格式或制作一个自定义函数,返回 1 -> 'st', 2-> 'nd' 3-> 'rd', any date no -> 'th'。
最佳答案
以下代码基于 answer从整数生成序数:
public static string ToOrdinal(int number)
{
switch(number % 100)
{
case 11:
case 12:
case 13:
return number.ToString() + "th";
}
switch(number % 10)
{
case 1:
return number.ToString() + "st";
case 2:
return number.ToString() + "nd";
case 3:
return number.ToString() + "rd";
default:
return number.ToString() + "th";
}
}
你可以生成你的输出字符串:
public static string GenerateDateString(DateTime value)
{
return string.Format(
"{0} {1:MMMM}",
ToOrdinal(value.Day),
value);
}
关于c# - 如何在 c# 中生成像 "1st November"这样的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4544256/