Skip to content

Commit 42964f6

Browse files
authored
fix(dates): add information for hour12 format pattern (#30) (#31)
1 parent 804628a commit 42964f6

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/dates.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ export interface DateFormatPart {
119119
* Specifies the format names options.
120120
*/
121121
names?: DateFormatNameOptions;
122+
123+
/**
124+
* Specifies whether a 12-hour time-set should be used for the formatting.
125+
*/
126+
hour12?: boolean;
122127
}
123128

124129
/**

src/dates/date-pattern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,4 @@ export default function datePattern(format, info) {
225225
}
226226

227227
return result;
228-
}
228+
}

src/dates/split-date-format.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ function addLiteral(parts, value) {
5252
}
5353
}
5454

55+
function isHour12(pattern) {
56+
return pattern === 'h';
57+
}
58+
5559
export default function splitDateFormat(format, locale = 'en') {
5660
const info = localeInfo(locale);
5761
const pattern = datePattern(format, info);
@@ -76,6 +80,10 @@ export default function splitDateFormat(format, locale = 'en') {
7680
pattern: value
7781
};
7882

83+
if (type === 'hour') {
84+
part.hour12 = isHour12(value);
85+
}
86+
7987
const names = NAME_TYPES[type];
8088

8189
if (names) {
@@ -103,4 +111,4 @@ export default function splitDateFormat(format, locale = 'en') {
103111
}
104112

105113
return parts;
106-
}
114+
}

test/dates.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,10 +1299,12 @@ describe('splitDateFormat', () => {
12991299
pattern: "eeee"
13001300
}, {
13011301
type: "hour",
1302-
pattern: "h"
1302+
pattern: "h",
1303+
hour12: true
13031304
}, {
13041305
type: "hour",
1305-
pattern: "HH"
1306+
pattern: "HH",
1307+
hour12: false
13061308
}, {
13071309
type: "minute",
13081310
pattern: "m"
@@ -1333,6 +1335,20 @@ describe('splitDateFormat', () => {
13331335

13341336
expect(splitDateFormat('GyyqQQQMMMMLLLLLdEEccceeeehHHmsaxXzZ')).toEqual(expected);
13351337
});
1338+
1339+
it('returns hour12 info in the DatePart', () => {
1340+
const expected = [{
1341+
type: 'hour',
1342+
pattern: 'h',
1343+
hour12: true
1344+
}, {
1345+
type: 'hour',
1346+
pattern: 'H',
1347+
hour12: false
1348+
}];
1349+
1350+
expect(splitDateFormat('hH')).toEqual(expected);
1351+
});
13361352
});
13371353

13381354

0 commit comments

Comments
 (0)