programming-examples/js/Date/Write a JavaScript function to get English ordinal suffix for the day of the month, 2 characters (st, nd, rd or th.)..js
2019-11-15 12:59:38 +01:00

10 lines
400 B
JavaScript

function english_ordinal_suffix(dt)
{
return dt.getDate()+(dt.getDate() % 10 == 1 && dt.getDate() != 11 ? 'st' : (dt.getDate() % 10 == 2 && dt.getDate() != 12 ? 'nd' : (dt.getDate() % 10 == 3 && dt.getDate() != 13 ? 'rd' : 'th')));
}
dt = new Date();
console.log(english_ordinal_suffix(dt));
dt = new Date(2015, 10, 1);
console.log(english_ordinal_suffix(dt));