如何生成“11 月 1 日”等日期格式在 c# 中

发布于 2024-10-09 06:22:34 字数 236 浏览 2 评论 0原文

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

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

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

我们可以使用任何日期格式或创建一个返回 1 -> 的自定义函数吗? 'st', 2-> 'nd' 3-> 'rd',任何日期无 -> “第”。

How can i get below mentions date format in c#.

  • For 1-Nov-2010 it should be display as : 1st November

  • For 30-Nov-2010 it should be display as : 30th November

Can we do using any date format or make a custom function that returns for 1 -> 'st', 2-> 'nd' 3-> 'rd', any date no -> 'th'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

耳根太软 2024-10-16 06:22:34

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

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);            
}

The following code is based on that answer that generates an ordinal from an integer:

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";
    }
}

Than you can generate your output string:

public static string GenerateDateString(DateTime value)
{
    return string.Format(
        "{0} {1:MMMM}",
        ToOrdinal(value.Day),
        value);            
}
柠檬心 2024-10-16 06:22:34

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

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;


     }
   }
}

Somthing like this should work...

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;


     }
   }
}
丘比特射中我 2024-10-16 06:22:34

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

希望有帮助。

So here is a fullish solution with extension methods. Works for C# 3.0 and above. Mostly plagiarized Nikhil's work:

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;
        }
}

Test/Use Like this:

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

Hope that Helps.

血之狂魔 2024-10-16 06:22:34

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

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

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

I'm pretty sure there's no datatime routine to show the date as 1st or 30th.

I recently wrote some code like that from scratch. I think you'll need to do the same.

I don't have my code handy but I just created a string array with the letters for each number ("th", "st", "nd", "rd", "th", etc.). Then mod against 10 and use the remainder as an index into the array. You can just append that string to your number.

不离久伴 2024-10-16 06:22:34

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

You can use Regular Expressions to extract the day and month. Then, store all the names of the months in an array and use the .startsWith to obtain the proper name of the month. You can use a simple case to see if you need the 'st', 'nd', 'rd' or 'th'.

那一片橙海, 2024-10-16 06:22:34

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

  return strNum + ordinal + str;

而不是!

  return ordinal + str;

I followed the string example blog from JSL vscontrol and it has a bug at the end he forgot to concatenate the day number at the beginning of the line so it should be

  return strNum + ordinal + str;

and not !

  return ordinal + str;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文