15

我怎样才能得到下面提到的 c# 中的日期格式。

  • 对于 2010 年 11 月 1 日,应显示为:11 月 1 日

  • 对于 2010 年 11 月 30 日,应显示为:11 月 30 日

我们可以使用任何日期格式或制作一个自定义函数,返回 1 -> 'st'、2-> 'nd' 3-> 'rd'、任何日期 no -> 'th'。

4

6 回答 6

32

以下代码基于从整数生成序数的答案

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);            
}
于 2010-12-28T07:11:29.213 回答
2

像这样的东西应该工作......

using System;
using System.Text;

namespace Program {


class Demo { 
      static string[] extensions = 
      //    0     1     2     3     4     5     6     7     8     9
         { "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th",
      //    10    11    12    13    14    15    16    17    18    19
           "th", "th", "th", "th", "th", "th", "th", "tn", "th", "th",
      //    20    21    22    23    24    25    26    27    28    29
           "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th",
      //    30    31
           "th", "st" };

  public static void Main() {
     String strTestDate = "02-11-2007";
     DateTime coverdate = DateTime.ParseExact(strTestDate, "dd-MM-yyyy", null);
     string s = coverdate.ToString(" MMMM yyyy");
     string t = string.Format("{0}{1}",coverdate.Day,extensions[coverdate.Day]);
     string result = t + s;


     }
   }
}
于 2010-12-28T07:10:58.823 回答
2

所以这是一个带有扩展方法的完整解决方案。适用于 C# 3.0 及更高版本。大部分抄袭Nikhil 的作品:

public static class DateTimeExtensions
{
        static string[] extensions = // 0 1 2 3 4 5 6 7 8 9 
            { "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th", 
                // 10 11 12 13 14 15 16 17 18 19 
                "th", "th", "th", "th", "th", "th", "th", "tn", "th", "th", 
                // 20 21 22 23 24 25 26 27 28 29 
                "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th", 
                // 30 31 
                "th", "st" 
            };
        public static string ToSpecialString(this DateTime dt)
        {
            string s = dt.ToString(" MMMM yyyy");
            string t = string.Format("{0}{1}", dt.Day, extensions[dt.Day]);
            return t + s;
        }
}

像这样测试/使用:

Console.WriteLine(DateTime.Now.ToSpecialString());
Console.WriteLine(new DateTime(1990, 11, 12).ToSpecialString());
Console.WriteLine(new DateTime(1990, 1, 1).ToSpecialString());
Console.WriteLine(new DateTime(1990, 1, 2).ToSpecialString());
Console.WriteLine(new DateTime(1990, 1, 3).ToSpecialString());
Console.WriteLine(new DateTime(1990, 1, 4).ToSpecialString());
Console.WriteLine(new DateTime(1990, 12, 15).ToSpecialString());
Console.WriteLine(new DateTime(1990, 8, 19).ToSpecialString());
Console.WriteLine(new DateTime(1990, 9, 22).ToSpecialString());
Console.ReadKey();

希望有帮助。

于 2010-12-28T07:31:03.327 回答
0

我很确定没有数据时间例程将日期显示为 1 日或 30 日。

我最近从头开始编写了一些类似的代码。我认为你也需要这样做。

我没有方便的代码,但我只是创建了一个字符串数组,其中包含每个数字的字母(“th”、“st”、“nd”、“rd”、“th”等)。然后对 10 进行 mod 并使用余数作为数组的索引。您只需将该字符串附加到您的号码即可。

于 2010-12-28T07:03:01.603 回答
0

您可以使用正则表达式来提取日期和月份。然后,将所有月份的名称存储在一个数组中,并使用.startsWith获取月份的正确名称。您可以使用简单case的来查看您是否需要'st'、'nd'、'rd'或'th'。

于 2010-12-28T07:07:30.923 回答
0

我遵循了来自 JSL vscontrol 的字符串示例博客,它最后有一个错误,他忘记在行首连接天数,所以它应该是

  return strNum + ordinal + str;

并不是 !

  return ordinal + str;
于 2015-09-03T23:05:40.083 回答