Displaying Ordinal Numbers In C# & .NET
[C#, .NET, StarLibrary, Humanizer]
Suppose you wanted to present some dates like this:
- 1st August 2025
- 2nd August 2025
- 3rd August 2025
- 4th August 2025
Or some other data like this:
- The 1st date
- The 2nd date
- The 3d date
- The 4th date
The parts 1st, 2nd, 3rd, 4th are called ordinal numerals.
Currently, there is no provision for this in any of the .NET format strings.
However, our old and trusted friend Humanizer can help with this problem.
For the dates, we can display them as follows:
DateOnly[] dates = [
new DateOnly(2025, 8, 1),
new DateOnly(2025, 8, 2),
new DateOnly(2025, 8, 3),
new DateOnly(2025, 8, 4)
];
foreach (var date in dates)
{
Console.WriteLine($"- {date.Day.Ordinalize()} {date:MMM} {date:yyyy}");
}
This will output the following:
- 1st Aug 2025
- 2nd Aug 2025
- 3rd Aug 2025
- 4th Aug 2025
The magic is happening in the Ordinalize()
method.
Our second example we can resolve like this:
var numbers = Enumerable.Range(1, 4);
foreach (var number in numbers)
{
Console.WriteLine($"- The {number.Ordinalize()} date");
}
This will print the following:
- The 1st date
- The 2nd date
- The 3rd date
- The 4th date
TLDR
Humanizer has an Orinalize()
method that allows you to print oridinal numbers.
Happy hacking!