【问题标题】:How Do I Produce a Date format like "1st November" in c#如何在 c# 中生成像“1st November”这样的日期格式
【发布时间】:2010-12-28 06:57:21
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# datetime formatting date-formatting


    【解决方案1】:

    以下代码基于从整数生成序数的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);            
    }
    

    【讨论】:

    • 它真的很棒。节省大量时间。 :)
    【解决方案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;
    
    
         }
       }
    }
    

    【讨论】:

      【解决方案3】:

      所以这是一个带有扩展方法的完整解决方案。适用于 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();
      

      希望有所帮助。

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

        • mod 对 10 还不够好,因为那样你就会有 11 和 12。
        • 是的,我知道我成功了。我想我对青少年有一些例外。
        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

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

            return strNum + ordinal + str;
          

          而不是!

            return ordinal + str;
          

          【讨论】:

          • 私有静态字符串 Ordinal(DateTime date) { int dayvalue = Convert.ToInt32(date.Day);字符串 strNum = dayvalue.ToString();字符串序数 = string.Empty; if (strNum.EndsWith("0") || strNum.EndsWith("4") || strNum.EndsWith("5") || strNum.EndsWith("6") || strNum.EndsWith("7") || strNum.EndsWith("8") || strNum.EndsWith("9")) { ordinal = "th"; } else if (strNum.EndsWith("1")) { ordinal = "st"; } else if (strNum.EndsWith("2")) { ordinal = "nd"; } else { 序数 = "rd"; } 字符串 str = date.ToString("MMMMM yyyy");返回序数+ str; }
          猜你喜欢
          • 2017-03-01
          • 2016-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-22
          • 2011-09-10
          • 1970-01-01
          • 2020-03-07
          相关资源
          最近更新 更多