How Do I Produce a Date format like quot;1st Novemberquot; in c#(如何生成像“1st November这样的日期格式?在c#中)
问题描述
我怎样才能得到下面提到的 c# 中的日期格式.
How can i get below mentions date format in c#.
对于 2010 年 11 月 1 日,应显示为:11 月 1 日
For 1-Nov-2010 it should be display as : 1st November
对于 2010 年 11 月 30 日,应显示为:11 月 30 日
For 30-Nov-2010 it should be display as : 30th November
我们可以使用任何日期格式或制作一个自定义函数,返回 1 -> 'st', 2-> 'nd' 3-> 'rd', any date no -> 'th'.
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'.
推荐答案
下面的代码基于 answer 从整数生成序数:
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);
}
这篇关于如何生成像“1st November"这样的日期格式?在c#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何生成像“1st November"这样的日期格式?在c#中


基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01