Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a DateTime value Day value

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?

like image 472
detaylor Avatar asked Mar 08 '11 17:03

detaylor


People also ask

How do you format date and day?

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.

What is the format of datetime?

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".

What are the format of datetime in SAS?

Writes datetime values in the form ddmmmyy:hh:mm:ss. ss.

What is T and Z in timestamp?

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).


1 Answers

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";
     }
 }
like image 67
jason Avatar answered Oct 27 '22 07:10

jason