Skip to content

Commit cf110b5

Browse files
DarkoKukovecBrandon Dail
authored andcommitted
feat: Add support for numeric months (#5)
1 parent b39a8e3 commit cf110b5

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ template.render(new Date());
2424

2525
* `MMMM` - Full Month (September)
2626
* `MM` - Partial Month (Sep)
27+
* `Mo` - Numeric Month (9)
2728
* `YYYY` - Full Year (1992)
2829
* `YY` - Partial Year (92)
2930
* `dddd` - Day of the Week (Monday)

__test__/index.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ describe('tinytime', () => {
2424
it('partial months', () => {
2525
expect(render('{MM}')).toEqual('Sep');
2626
});
27+
it('numeric months', () => {
28+
expect(render('{Mo}')).toEqual('9');
29+
});
30+
it('padded numeric months', () => {
31+
const template = tinytime('{Mo}', { padMonth: true })
32+
const rendered = template.render(date);
33+
expect(rendered).toEqual('09');
34+
});
2735
it('full years', () => {
2836
expect(render('{YYYY}')).toEqual('1992');
2937
});

src/compiler.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import {
44
FullMonth,
55
PartialMonth,
6+
NumberMonth,
67
FullYear,
78
PartialYear,
89
DayOfTheWeek,
@@ -105,6 +106,13 @@ export default function compiler(tokens: Array<Token>, date: Date, options: Tiny
105106
case FullMonth:
106107
compiled += months[month];
107108
break;
109+
case NumberMonth:
110+
let mnth = month + 1;
111+
if (options.padMonth) {
112+
mnth = paddWithZeros(mnth);
113+
}
114+
compiled += mnth;
115+
break;
108116
case FullYear:
109117
compiled += year;
110118
break;

src/subs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ export const Seconds = 'i';
1818
export const PostOrAnteMeridiem = 'j';
1919
export const Day = 'k';
2020
export const DayOfTheMonth = 'l';
21+
export const NumberMonth = 'n';
2122
export const Hour24 = 'm';
2223

2324
const SubToTypeIdentifierMap: {
2425
[abbreviation: string]: string
2526
} = {
2627
'MMMM': FullMonth,
2728
'MM': PartialMonth,
29+
'Mo': NumberMonth,
2830
'YYYY': FullYear,
2931
'YY': PartialYear,
3032
'dddd': DayOfTheWeek,

0 commit comments

Comments
 (0)