在使用DateTime.ToString()时获取星期后缀

使用DateTime.ToString()格式化date时,是否可以包含date后缀?

例如,我想用以下格式打印date – 2009年7月27日星期一。但是我能够使用DateTime.ToString()find的最接近的例子是2009年7月27日星期一。

我可以用DateTime.ToString()做到这一点,或者我将不得不回到我自己的代码?

作为一个参考,我总是使用/参考SteveXstring格式,并且在任何可用的variables中都没有任何“th”,但是您可以轻松地构build一个string

string.Format("{0:dddd dd}{1} {0:MMMM yyyy}", DateTime.Now, (?)); 

然后你必须提供一个“st”为1,“nd”为2,“rd”为3,“th”为所有其他,并且可以内置一个“?:”语句。

 (DateTime.Now.Day % 10 == 1 && DateTime.Now.Day != 11) ? "st" : (DateTime.Now.Day % 10 == 2 && DateTime.Now.Day != 12) ? "nd" : (DateTime.Now.Day % 10 == 3 && DateTime.Now.Day != 13) ? "rd" : "th" 

另一个select使用开关:

 string GetDaySuffix(int day) { switch (day) { case 1: case 21: case 31: return "st"; case 2: case 22: return "nd"; case 3: case 23: return "rd"; default: return "th"; } } 

使用几个扩展方法:

 namespace System { public static class IntegerExtensions { public static string ToOccurrenceSuffix(this int integer) { switch (integer % 100) { case 11: case 12: case 13: return "th"; } switch (integer % 10) { case 1: return "st"; case 2: return "nd"; case 3: return "rd"; default: return "th"; } } } public static class DateTimeExtensions { public static string ToString(this DateTime dateTime, string format, bool useExtendedSpecifiers) { return dateTime.ToString(format) .Replace("nn", dateTime.Day.ToOccurrenceSuffix().ToLower()) .Replace("NN", dateTime.Day.ToOccurrenceSuffix().ToUpper()); } } } 

用法:

 return DateTime.Now.ToString("dddd, dnn MMMM yyyy", useExtendedSpecifiers: true); // Friday, 7th March 2014 

注意:整数扩展方法可以用于任何数字,而不仅仅是1到31.例如,

 return 332211.ToOccurrenceSuffix(); // th 

另一个select是使用Modulo Operator :

 public string CreateDateSuffix(DateTime date) { // Get day... var day = date.Day; // Get day modulo... var dayModulo = day%10; // Convert day to string... var suffix = day.ToString(CultureInfo.InvariantCulture); // Combine day with correct suffix... suffix += (day == 11 || day == 12 || day == 13) ? "th" : (dayModulo == 1) ? "st" : (dayModulo == 2) ? "nd" : (dayModulo == 3) ? "rd" : "th"; // Return result... return suffix; } 

这里是扩展版本,包括11日,12日和13日:

 DateTime dt = DateTime.Now; string d2d = dt.ToString("dd").Substring(1); string daySuffix = (dt.Day == 11 || dt.Day == 12 || dt.Day == 13) ? "th" : (d2d == "1") ? "st" : (d2d == "2") ? "nd" : (d2d == "3") ? "rd" : "th"; 

这里有一个扩展方法(因为每个人都喜欢扩展方法),以Lazlow的答案为基础(Lazlow's认为它易于阅读)。

DateTime上的常规ToString()方法一样,除了如果格式包含ddd ,则后缀将自动添加。

 /// <summary> /// Return a DateTime string with suffix eg "st", "nd", "rd", "th" /// So a format "dd-MMM-yyyy" could return "16th-Jan-2014" /// </summary> public static string ToStringWithSuffix(this DateTime dateTime, string format, string suffixPlaceHolder = "$") { if(format.LastIndexOf("d", StringComparison.Ordinal) == -1 || format.Count(x => x == 'd') > 2) { return dateTime.ToString(format); } string suffix; switch(dateTime.Day) { case 1: case 21: case 31: suffix = "st"; break; case 2: case 22: suffix = "nd"; break; case 3: case 23: suffix = "rd"; break; default: suffix = "th"; break; } var formatWithSuffix = format.Insert(format.LastIndexOf("d", StringComparison.InvariantCultureIgnoreCase) + 1, suffixPlaceHolder); var date = dateTime.ToString(formatWithSuffix); return date.Replace(suffixPlaceHolder, suffix); } 

以@ Lazlow的答案为一个完整的解决scheme,以下是一个完全可重用的扩展方法,

 internal static string HumanisedDate(this DateTime date) { string ordinal; switch (date.Day) { case 1: case 21: case 31: ordinal = "st"; break; case 2: case 22: ordinal = "nd"; break; case 3: case 23: ordinal = "rd"; break; default: ordinal = "th"; break; } return string.Format("{0:dddd dd}{1} {0:MMMM yyyy}", date, ordinal); } 

要使用这个,你只需要在DateTime对象上调用它;

 var myDate = DateTime.Now(); var myDateString = myDate.HumanisedFormat() 

哪个会给你:

2016年6月17日,星期五

我相信这是一个很好的解决scheme,涵盖了第111等数字:

 private string daySuffix(int day) { if (day > 0) { if (day % 10 == 1 && day % 100 != 11) return "st"; else if (day % 10 == 2 && day % 100 != 12) return "nd"; else if (day % 10 == 3 && day % 100 != 13) return "rd"; else return "th"; } else return string.Empty; } 

我是这样做的,它解决了其他例子中给出的一些问题。

  public static string TwoLetterSuffix(this DateTime @this) { var dayMod10 = @this.Day % 10; if (dayMod10 > 3 || dayMod10 == 0 || (@this.Day >= 10 && @this.Day <= 19)) { return "th"; } else if(dayMod10 == 1) { return "st"; } else if (dayMod10 == 2) { return "nd"; } else { return "rd"; } } 

一个便宜和开朗的VB解决scheme:

 litDate.Text = DatePart("dd", Now) & GetDateSuffix(DatePart("dd", Now)) Function GetDateSuffix(ByVal dateIn As Integer) As String '// returns formatted date suffix Dim dateSuffix As String = "" Select Case dateIn Case 1, 21, 31 dateSuffix = "st" Case 2, 22 dateSuffix = "nd" Case 3, 23 dateSuffix = "rd" Case Else dateSuffix = "th" End Select Return dateSuffix End Function 

对于它的价值在这里是我的最终解决scheme使用下面的答案

  DateTime dt = DateTime.Now; string d2d = dt.ToString("dd").Substring(1); string suffix = (dt.Day == 11 || dt.Day == 12 || dt.Day == 13) ? "th" : (d2d == "1") ? "st" : (d2d == "2") ? "nd" : (d2d == "3") ? "rd" : "th"; Date.Text = DateTime.Today.ToString("dddd d") + suffix + " " + DateTime.Today.ToString("MMMM") + DateTime.Today.ToString(" yyyy"); 

public static String SuffixDate(DateTime date){string ordinal;

  switch (date.Day) { case 1: case 21: case 31: ordinal = "st"; break; case 2: case 22: ordinal = "nd"; break; case 3: case 23: ordinal = "rd"; break; default: ordinal = "th"; break; } if (date.Day < 10) return string.Format("{0:d}{2} {1:MMMM yyyy}", date.Day, date, ordinal); else return string.Format("{0:dd}{1} {0:MMMM yyyy}", date, ordinal); } 

在MSDN文档中,没有提到可以将17转换成17的文化。 所以你应该手动通过代码隐藏。或者build立一个…你可以build立一个function,做到这一点。

 public string CustomToString(this DateTime date) { string dateAsString = string.empty; <here wright your code to convert 17 to 17th> return dateAsString; } 

另一个使用最后一个string的选项:

 public static string getDayWithSuffix(int day) { string d = day.ToString(); if (day < 11 || day > 13) { if (d.EndsWith("1")) { d += "st"; } else if (d.EndsWith("2")) { d += "nd"; } else if (d.EndsWith("3")) { d += "rd"; } else { d += "th"; } else { d += "th"; } return d; }