Skip to content
Browse files

implement %o to get the day of the month as an ordinal

  • Loading branch information...
1 parent e02de27 commit 088600d67f6d94be751b7c950b1fab8f6ce217dc @samsonjs committed May 15, 2013
Showing with 18 additions and 0 deletions.
  1. +1 −0 Readme.md
  2. +16 −0 strftime.js
  3. +1 −0 test/test.js
View
1 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`
View
16 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.
View
1 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')

0 comments on commit 088600d

Please sign in to comment.
Something went wrong with that request. Please try again.