Is there a simple method of displaying the day portion of a date in the format 1st, 2nd, 3rd,…? I suspect that there is no method of doing this through custom datetime formatstrings (I will be very happy to be wrong), so has anyone implemented a way of doing this?
American usage calls for a month/day/year date format, the United Kingdom and much of Europe use a day/month/year format, and most countries in Asia use the year/month/day format.
For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".
Writes datetime values in the form ddmmmyy:hh:mm:ss. ss.
The T doesn't really stand for anything. It is just the separator that the ISO 8601 combined date-time format requires. You can read it as an abbreviation for Time. The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).
This is the core logic for achieving this end:
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";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With