If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:
<
Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.
function getOrdinalNum(n) {
return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');
}
Update 2020-06-23. The following is a better readable answer of the function above:
const getOrdinalNum = (number) => {
let selector;
if (number <= 0) {
selector = 4;
} else if ((number > 3 && number < 21) || number % 10 > 3) {
selector = 0;
} else {
selector = number % 10;
}
return number + ['th', 'st', 'nd', 'rd', ''][selector];
};