mirror of
https://github.com/samsonjs/strftime.git
synced 2026-04-27 14:57:37 +00:00
implement %o to get the day of the month as an ordinal
This commit is contained in:
parent
e02de2786d
commit
088600d67f
3 changed files with 18 additions and 0 deletions
|
|
@ -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 minute, padded to 2 digits (00-59)
|
||||||
- m: the month, padded to 2 digits (01-12)
|
- m: the month, padded to 2 digits (01-12)
|
||||||
- n: newline character
|
- 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" in lowercase [Ruby extension]
|
||||||
- p: "AM" or "PM"
|
- p: "AM" or "PM"
|
||||||
- R: equivalent to `%H:%M`
|
- R: equivalent to `%H:%M`
|
||||||
|
|
|
||||||
16
strftime.js
16
strftime.js
|
|
@ -122,6 +122,7 @@
|
||||||
case 'M': return pad(d.getMinutes(), padding);
|
case 'M': return pad(d.getMinutes(), padding);
|
||||||
case 'm': return pad(d.getMonth() + 1, padding);
|
case 'm': return pad(d.getMonth() + 1, padding);
|
||||||
case 'n': return '\n';
|
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 '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);
|
case 'R': return _strftime(locale.formats.R || '%H:%M', d, locale);
|
||||||
|
|
@ -204,6 +205,21 @@
|
||||||
return hour;
|
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'
|
// firstWeekday: 'sunday' or 'monday', default is 'sunday'
|
||||||
//
|
//
|
||||||
// Pilfered & ported from Ruby's strftime implementation.
|
// Pilfered & ported from Ruby's strftime implementation.
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,7 @@ assert.format('%0l', null, '06')
|
||||||
assert.format('%M', null, '51')
|
assert.format('%M', null, '51')
|
||||||
assert.format('%m', '06')
|
assert.format('%m', '06')
|
||||||
assert.format('%n', '\n')
|
assert.format('%n', '\n')
|
||||||
|
assert.format('%o', '7th')
|
||||||
assert.format('%P', null, 'pm')
|
assert.format('%P', null, 'pm')
|
||||||
assert.format('%p', null, 'PM')
|
assert.format('%p', null, 'PM')
|
||||||
assert.format('%R', null, '18:51')
|
assert.format('%R', null, '18:51')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue