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

Commit b5bb2eb

Browse files
committed
feat(dateParser): Add support for HH, H, mm, m, ss, s formats
- Add support for `HH`, `H`, `mm`, `m`, `ss`, `s` formats from Angular's `dateFilter` - Add support for `:` character in format expression
1 parent f9a9b97 commit b5bb2eb

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

Diff for: src/dateparser/dateparser.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ angular.module('ui.bootstrap.dateparser', [])
4646
},
4747
'EEE': {
4848
regex: $locale.DATETIME_FORMATS.SHORTDAY.join('|')
49+
},
50+
'HH': {
51+
regex: '(?:0|1)[0-9]|2[0-3]',
52+
apply: function(value) { this.hours = +value; }
53+
},
54+
'H': {
55+
regex: '1?[0-9]|2[0-3]',
56+
apply: function(value) { this.hours = +value; }
57+
},
58+
'mm': {
59+
regex: '[0-5][0-9]',
60+
apply: function(value) { this.minutes = +value; }
61+
},
62+
'm': {
63+
regex: '[0-9]|[1-5][0-9]',
64+
apply: function(value) { this.minutes = +value; }
65+
},
66+
'ss': {
67+
regex: '[0-5][0-9]',
68+
apply: function(value) { this.seconds = +value; }
69+
},
70+
's': {
71+
regex: '[0-9]|[1-5][0-9]',
72+
apply: function(value) { this.seconds = +value; }
4973
}
5074
};
5175

@@ -81,7 +105,7 @@ angular.module('ui.bootstrap.dateparser', [])
81105
return input;
82106
}
83107

84-
format = $locale.DATETIME_FORMATS[format] || format;
108+
format = $locale.DATETIME_FORMATS[format] || format.replace(/:/g, '\\:');
85109

86110
if ( !this.parsers[format] ) {
87111
this.parsers[format] = createParser(format);
@@ -93,7 +117,7 @@ angular.module('ui.bootstrap.dateparser', [])
93117
results = input.match(regex);
94118

95119
if ( results && results.length ) {
96-
var fields = { year: 1900, month: 0, date: 1, hours: 0 }, dt;
120+
var fields = { year: 1900, month: 0, date: 1, hours: 0, minutes: 0, seconds: 0 }, dt;
97121

98122
for( var i = 1, n = results.length; i < n; i++ ) {
99123
var mapper = map[i-1];
@@ -103,7 +127,7 @@ angular.module('ui.bootstrap.dateparser', [])
103127
}
104128

105129
if ( isValid(fields.year, fields.month, fields.date) ) {
106-
dt = new Date( fields.year, fields.month, fields.date, fields.hours);
130+
dt = new Date(fields.year, fields.month, fields.date, fields.hours, fields.minutes, fields.seconds);
107131
}
108132

109133
return dt;

Diff for: src/dateparser/test/dateparser.spec.js

+56
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,62 @@ describe('date parser', function () {
5959
expectParse('1955/February/5', 'yyyy/MMMM/d', new Date(1955, 1, 5, 0));
6060
expectParse('11-08-13', 'd-MM-yy', new Date(2013, 7, 11, 0));
6161
});
62+
63+
it('should work correctly for `HH`', function() {
64+
expectParse('22.March.15.22', 'd.MMMM.yy.HH', new Date(2015, 2, 22, 22));
65+
expectParse('8-March-1991-11', 'd-MMMM-yyyy-HH', new Date(1991, 2, 8, 11));
66+
expectParse('February/5/1980/00', 'MMMM/d/yyyy/HH', new Date(1980, 1, 5, 0));
67+
expectParse('1955/February/5 03', 'yyyy/MMMM/d HH', new Date(1955, 1, 5, 3));
68+
expectParse('11-08-13 23', 'd-MM-yy HH', new Date(2013, 7, 11, 23));
69+
});
70+
71+
it('should work correctly for `H`', function() {
72+
expectParse('22.March.15.22', 'd.MMMM.yy.H', new Date(2015, 2, 22, 22));
73+
expectParse('8-March-1991-11', 'd-MMMM-yyyy-H', new Date(1991, 2, 8, 11));
74+
expectParse('February/5/1980/0', 'MMMM/d/yyyy/H', new Date(1980, 1, 5, 0));
75+
expectParse('1955/February/5 3', 'yyyy/MMMM/d H', new Date(1955, 1, 5, 3));
76+
expectParse('11-08-13 23', 'd-MM-yy H', new Date(2013, 7, 11, 23));
77+
});
78+
79+
it('should work correctly for `mm`', function() {
80+
expectParse('22.March.15.22', 'd.MMMM.yy.mm', new Date(2015, 2, 22, 0, 22));
81+
expectParse('8-March-1991-59', 'd-MMMM-yyyy-mm', new Date(1991, 2, 8, 0, 59));
82+
expectParse('February/5/1980/00', 'MMMM/d/yyyy/mm', new Date(1980, 1, 5, 0, 0));
83+
expectParse('1955/February/5 03', 'yyyy/MMMM/d mm', new Date(1955, 1, 5, 0, 3));
84+
expectParse('11-08-13 46', 'd-MM-yy mm', new Date(2013, 7, 11, 0, 46));
85+
expectParse('22.March.15.22:33', 'd.MMMM.yy.HH:mm', new Date(2015, 2, 22, 22, 33));
86+
expectParse('22.March.15.2:01', 'd.MMMM.yy.H:mm', new Date(2015, 2, 22, 2, 1));
87+
});
88+
89+
it('should work correctly for `m`', function() {
90+
expectParse('22.March.15.22', 'd.MMMM.yy.m', new Date(2015, 2, 22, 0, 22));
91+
expectParse('8-March-1991-59', 'd-MMMM-yyyy-m', new Date(1991, 2, 8, 0, 59));
92+
expectParse('February/5/1980/0', 'MMMM/d/yyyy/m', new Date(1980, 1, 5, 0, 0));
93+
expectParse('1955/February/5 3', 'yyyy/MMMM/d m', new Date(1955, 1, 5, 0, 3));
94+
expectParse('11-08-13 46', 'd-MM-yy m', new Date(2013, 7, 11, 0, 46));
95+
expectParse('22.March.15.22:3', 'd.MMMM.yy.HH:m', new Date(2015, 2, 22, 22, 3));
96+
expectParse('22.March.15.2:1', 'd.MMMM.yy.H:m', new Date(2015, 2, 22, 2, 1));
97+
});
98+
99+
it('should work correctly for `ss`', function() {
100+
expectParse('22.March.15.22', 'd.MMMM.yy.ss', new Date(2015, 2, 22, 0, 0, 22));
101+
expectParse('8-March-1991-59', 'd-MMMM-yyyy-ss', new Date(1991, 2, 8, 0, 0, 59));
102+
expectParse('February/5/1980/00', 'MMMM/d/yyyy/ss', new Date(1980, 1, 5, 0, 0, 0));
103+
expectParse('1955/February/5 03', 'yyyy/MMMM/d ss', new Date(1955, 1, 5, 0, 0, 3));
104+
expectParse('11-08-13 46', 'd-MM-yy ss', new Date(2013, 7, 11, 0, 0, 46));
105+
expectParse('22.March.15.22:33:44', 'd.MMMM.yy.HH:mm:ss', new Date(2015, 2, 22, 22, 33, 44));
106+
expectParse('22.March.15.0:0:01', 'd.MMMM.yy.H:m:ss', new Date(2015, 2, 22, 0, 0, 1));
107+
});
108+
109+
it('should work correctly for `s`', function() {
110+
expectParse('22.March.15.22', 'd.MMMM.yy.s', new Date(2015, 2, 22, 0, 0, 22));
111+
expectParse('8-March-1991-59', 'd-MMMM-yyyy-s', new Date(1991, 2, 8, 0, 0, 59));
112+
expectParse('February/5/1980/0', 'MMMM/d/yyyy/s', new Date(1980, 1, 5, 0, 0, 0));
113+
expectParse('1955/February/5 3', 'yyyy/MMMM/d s', new Date(1955, 1, 5, 0, 0, 3));
114+
expectParse('11-08-13 46', 'd-MM-yy s', new Date(2013, 7, 11, 0, 0, 46));
115+
expectParse('22.March.15.22:33:4', 'd.MMMM.yy.HH:mm:s', new Date(2015, 2, 22, 22, 33, 4));
116+
expectParse('22.March.15.22:3:4', 'd.MMMM.yy.HH:m:s', new Date(2015, 2, 22, 22, 3, 4));
117+
});
62118
});
63119

64120
describe('wih predefined formats', function() {

0 commit comments

Comments
 (0)