diff --git a/Readme.md b/Readme.md index 54108bb..8f8bf1d 100644 --- a/Readme.md +++ b/Readme.md @@ -73,6 +73,7 @@ e.g. `%q` becomes `q`. Use `%%` to get a literal `%` sign. - M: the minute, padded to 2 digits (00-59) - m: the month, padded to 2 digits (01-12) - n: newline character +- o: day of the month as an ordinal (without padding), e.g. 1st, 2nd, 3rd, 4th, ... - P: "am" or "pm" in lowercase [Ruby extension] - p: "AM" or "PM" - R: equivalent to `%H:%M` diff --git a/strftime.js b/strftime.js index 217fae7..7ea7aee 100644 --- a/strftime.js +++ b/strftime.js @@ -122,6 +122,7 @@ case 'M': return pad(d.getMinutes(), padding); case 'm': return pad(d.getMonth() + 1, padding); case 'n': return '\n'; + case 'o': return String(d.getDate()) + ordinal(d.getDate()); case 'P': return d.getHours() < 12 ? locale.am : locale.pm; case 'p': return d.getHours() < 12 ? locale.AM : locale.PM; case 'R': return _strftime(locale.formats.R || '%H:%M', d, locale); @@ -204,6 +205,21 @@ return hour; } + // Get the ordinal suffix for a number: st, nd, rd, or th + function ordinal(n) { + var i = n % 10 + , ii = n % 100 + ; + if ((ii >= 11 && ii <= 13) || i === 0 || i >= 4) { + return 'th'; + } + switch (i) { + case 1: return 'st'; + case 2: return 'nd'; + case 3: return 'rd'; + } + } + // firstWeekday: 'sunday' or 'monday', default is 'sunday' // // Pilfered & ported from Ruby's strftime implementation. diff --git a/test/test.js b/test/test.js index 8855f35..3df8fdf 100755 --- a/test/test.js +++ b/test/test.js @@ -84,6 +84,7 @@ assert.format('%0l', null, '06') assert.format('%M', null, '51') assert.format('%m', '06') assert.format('%n', '\n') +assert.format('%o', '7th') assert.format('%P', null, 'pm') assert.format('%p', null, 'PM') assert.format('%R', null, '18:51')