Formatting a DateTime Value Day Value

Is there an easy way to display the day part of a date in the format 1, 2, 3, ...? I suspect there is no way to do this using custom date and time formatting (I will be very happy to make a mistake), so has anyone implemented a way to do this?

+5
source share
2 answers

This is the basic logic for achieving this goal:

string SuffixForDay(DateTime date) {
    switch (date.Day) {
        case 1:
        case 21:
        case 31:
            return "st";
        case 2:
        case 22:
            return "nd";
        case 3:
        case 23:
            return "rd";
        default:
            return "th";
     }
 }
+9
source

You can use the DateTime.Day property for this purpose.

0
source

All Articles