Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 1ecd82c

Browse files
th-kjohnsonwesleycho
authored andcommitted
fix(dateparser): Support 12-hour format and AM/PM
- Adds support for the flags `hh` and `a` Closes #4117
1 parent e633ca8 commit 1ecd82c

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/dateparser/dateparser.js

+16
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ angular.module('ui.bootstrap.dateparser', [])
5353
regex: '(?:0|1)[0-9]|2[0-3]',
5454
apply: function(value) { this.hours = +value; }
5555
},
56+
'hh': {
57+
regex: '0[0-9]|1[0-2]',
58+
apply: function(value) { this.hours = +value; }
59+
},
5660
'H': {
5761
regex: '1?[0-9]|2[0-3]',
5862
apply: function(value) { this.hours = +value; }
@@ -76,6 +80,18 @@ angular.module('ui.bootstrap.dateparser', [])
7680
's': {
7781
regex: '[0-9]|[1-5][0-9]',
7882
apply: function(value) { this.seconds = +value; }
83+
},
84+
'a': {
85+
regex: $locale.DATETIME_FORMATS.AMPMS.join('|'),
86+
apply: function(value) {
87+
if (this.hours === 12) {
88+
this.hours = 0;
89+
}
90+
91+
if (value === 'PM') {
92+
this.hours += 12;
93+
}
94+
}
7995
}
8096
};
8197

src/dateparser/test/dateparser.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ describe('date parser', function () {
6868
expectParse('11-08-13 23', 'd-MM-yy HH', new Date(2013, 7, 11, 23));
6969
});
7070

71+
it('should work correctly for `hh`', function() {
72+
expectParse('22.March.15.22', 'd.MMMM.yy.hh', undefined);
73+
expectParse('22.March.15.12', 'd.MMMM.yy.hh', new Date(2015, 2, 22, 12));
74+
expectParse('8-March-1991-11', 'd-MMMM-yyyy-HH', new Date(1991, 2, 8, 11));
75+
expectParse('February/5/1980/00', 'MMMM/d/yyyy/hh', new Date(1980, 1, 5, 0));
76+
expectParse('1955/February/5 03', 'yyyy/MMMM/d hh', new Date(1955, 1, 5, 3));
77+
expectParse('11-08-13 23', 'd-MM-yy hh', undefined);
78+
expectParse('11-08-13 09', 'd-MM-yy hh', new Date(2013, 7, 11, 9));
79+
});
80+
7181
it('should work correctly for `H`', function() {
7282
expectParse('22.March.15.22', 'd.MMMM.yy.H', new Date(2015, 2, 22, 22));
7383
expectParse('8-March-1991-11', 'd-MMMM-yyyy-H', new Date(1991, 2, 8, 11));
@@ -125,6 +135,19 @@ describe('date parser', function () {
125135
expectParse('22.March.15.22:33:4', 'd.MMMM.yy.HH:mm:s', new Date(2015, 2, 22, 22, 33, 4));
126136
expectParse('22.March.15.22:3:4', 'd.MMMM.yy.HH:m:s', new Date(2015, 2, 22, 22, 3, 4));
127137
});
138+
139+
it('should work correctly for `a`', function() {
140+
expectParse('22.March.15.10AM', 'd.MMMM.yy.hha', new Date(2015, 2, 22, 10));
141+
expectParse('22.March.15.10PM', 'd.MMMM.yy.hha', new Date(2015, 2, 22, 22));
142+
expectParse('8-March-1991-11AM', 'd-MMMM-yyyy-hha', new Date(1991, 2, 8, 11));
143+
expectParse('8-March-1991-11PM', 'd-MMMM-yyyy-hha', new Date(1991, 2, 8, 23));
144+
expectParse('February/5/1980/12AM', 'MMMM/d/yyyy/hha', new Date(1980, 1, 5, 0));
145+
expectParse('February/5/1980/12PM', 'MMMM/d/yyyy/hha', new Date(1980, 1, 5, 12));
146+
expectParse('1955/February/5 03AM', 'yyyy/MMMM/d hha', new Date(1955, 1, 5, 3));
147+
expectParse('1955/February/5 03PM', 'yyyy/MMMM/d hha', new Date(1955, 1, 5, 15));
148+
expectParse('11-08-13 09AM', 'd-MM-yy hha', new Date(2013, 7, 11, 9));
149+
expectParse('11-08-13 09PM', 'd-MM-yy hha', new Date(2013, 7, 11, 21));
150+
});
128151
});
129152

130153
describe('with predefined formats', function() {

0 commit comments

Comments
 (0)