|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
Format a date to include english date suffix
Hi,
Can anyone tell me the correct parameter to format a date with english date suffixes in c#. (e.g 3rd February) I can't seem to find a decent example from seraching the net. Many Thanks |
|
#2
|
||||
|
||||
|
There is no built in function for that. Basically the best way to do it is to write something yourself to pump out the suffix, then format the date/time string accordingly.
-Mbirchmeier
__________________
I have noticed that the devshed spell check sugggests that MBirchmeier is a misspelling for 'bitchier'. Apparently even computers have freudian slips. 0x4279 7465 204D 6521 |
|
#3
|
|||
|
|||
|
Quote:
o.k. Thanks very much for the reply |
|
#4
|
|||
|
|||
|
I wrote the following code to do exactly this...
Code:
// Use "~" for English Date Suffix
public static string CustomDateFormat(string format, DateTime date)
{
string[] arr = format.Split('~');
string suffix = GetDateSuffix(date);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.Length; i++)
{
sb.Append(string.Format("{0:" + arr[i] + "}", date));
if (i < arr.Length - 1)
{
sb.Append(suffix);
}
}
return sb.ToString();
}
public static string GetDateSuffix(DateTime date)
{
string day = date.Day.ToString();
if (day.EndsWith("1"))
{
return day.StartsWith("1") && date.Day != 1 ? "th" : "st";
}
else if (day.EndsWith("2"))
{
return day.StartsWith("1") ? "th" : "nd";
}
else if (day.EndsWith("3"))
{
return day.StartsWith("1") ? "th" : "rd";
}
return "th";
}
You can use it as follows: Code:
CustomDateFormat("dddd d~ MMMM yyyy", DateTime.Now)
|
|
#5
|
|||
|
|||
|
Thanks Hainesy,I am using it on my Project.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > .Net Development > Format a date to include english date suffix |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|