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

Commit 971a1b5

Browse files
wesleychochrisirhc
authored andcommitted
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 - Fix typos - Add regexp escaping of special characters Fixes #2509 Fixes #3159 Closes #3417
1 parent b4bbc01 commit 971a1b5

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

src/dateparser/dateparser.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
angular.module('ui.bootstrap.dateparser', [])
22

33
.service('dateParser', ['$locale', 'orderByFilter', function($locale, orderByFilter) {
4+
// Pulled from https://github.com/mbostock/d3/blob/master/src/format/requote.js
5+
var SPECIAL_CHARACTERS_REGEXP = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;
46

57
this.parsers = {};
68

@@ -46,6 +48,30 @@ angular.module('ui.bootstrap.dateparser', [])
4648
},
4749
'EEE': {
4850
regex: $locale.DATETIME_FORMATS.SHORTDAY.join('|')
51+
},
52+
'HH': {
53+
regex: '(?:0|1)[0-9]|2[0-3]',
54+
apply: function(value) { this.hours = +value; }
55+
},
56+
'H': {
57+
regex: '1?[0-9]|2[0-3]',
58+
apply: function(value) { this.hours = +value; }
59+
},
60+
'mm': {
61+
regex: '[0-5][0-9]',
62+
apply: function(value) { this.minutes = +value; }
63+
},
64+
'm': {
65+
regex: '[0-9]|[1-5][0-9]',
66+
apply: function(value) { this.minutes = +value; }
67+
},
68+
'ss': {
69+
regex: '[0-5][0-9]',
70+
apply: function(value) { this.seconds = +value; }
71+
},
72+
's': {
73+
regex: '[0-9]|[1-5][0-9]',
74+
apply: function(value) { this.seconds = +value; }
4975
}
5076
};
5177

@@ -82,6 +108,7 @@ angular.module('ui.bootstrap.dateparser', [])
82108
}
83109

84110
format = $locale.DATETIME_FORMATS[format] || format;
111+
format = format.replace(SPECIAL_CHARACTERS_REGEXP, '\\$&');
85112

86113
if ( !this.parsers[format] ) {
87114
this.parsers[format] = createParser(format);
@@ -93,7 +120,7 @@ angular.module('ui.bootstrap.dateparser', [])
93120
results = input.match(regex);
94121

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

98125
for( var i = 1, n = results.length; i < n; i++ ) {
99126
var mapper = map[i-1];
@@ -103,7 +130,7 @@ angular.module('ui.bootstrap.dateparser', [])
103130
}
104131

105132
if ( isValid(fields.year, fields.month, fields.date) ) {
106-
dt = new Date( fields.year, fields.month, fields.date, fields.hours);
133+
dt = new Date(fields.year, fields.month, fields.date, fields.hours, fields.minutes, fields.seconds);
107134
}
108135

109136
return dt;

src/dateparser/test/dateparser.spec.js

+58-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('date parser', function () {
1010
expect(dateParser.parse(input, format)).toEqual(date);
1111
}
1212

13-
describe('wih custom formats', function() {
13+
describe('with custom formats', function() {
1414
it('should work correctly for `dd`, `MM`, `yyyy`', function() {
1515
expectParse('17.11.2013', 'dd.MM.yyyy', new Date(2013, 10, 17, 0));
1616
expectParse('31.12.2013', 'dd.MM.yyyy', new Date(2013, 11, 31, 0));
@@ -59,9 +59,65 @@ 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

64-
describe('wih predefined formats', function() {
120+
describe('with predefined formats', function() {
65121
it('should work correctly for `shortDate`', function() {
66122
expectParse('9/3/10', 'shortDate', new Date(2010, 8, 3, 0));
67123
});

0 commit comments

Comments
 (0)