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

Commit e049bbb

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 - Fix typos - Add regexp escaping of special characters - remove unnecessary code
1 parent f9a9b97 commit e049bbb

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

Diff for: src/dateparser/dateparser.js

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

33
.service('dateParser', ['$locale', 'orderByFilter', function($locale, orderByFilter) {
4-
54
this.parsers = {};
65

76
var formatCodeToRegex = {
@@ -46,6 +45,30 @@ angular.module('ui.bootstrap.dateparser', [])
4645
},
4746
'EEE': {
4847
regex: $locale.DATETIME_FORMATS.SHORTDAY.join('|')
48+
},
49+
'HH': {
50+
regex: '(?:0|1)[0-9]|2[0-3]',
51+
apply: function(value) { this.hours = +value; }
52+
},
53+
'H': {
54+
regex: '1?[0-9]|2[0-3]',
55+
apply: function(value) { this.hours = +value; }
56+
},
57+
'mm': {
58+
regex: '[0-5][0-9]',
59+
apply: function(value) { this.minutes = +value; }
60+
},
61+
'm': {
62+
regex: '[0-9]|[1-5][0-9]',
63+
apply: function(value) { this.minutes = +value; }
64+
},
65+
'ss': {
66+
regex: '[0-5][0-9]',
67+
apply: function(value) { this.seconds = +value; }
68+
},
69+
's': {
70+
regex: '[0-9]|[1-5][0-9]',
71+
apply: function(value) { this.seconds = +value; }
4972
}
5073
};
5174

@@ -93,7 +116,7 @@ angular.module('ui.bootstrap.dateparser', [])
93116
results = input.match(regex);
94117

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

98121
for( var i = 1, n = results.length; i < n; i++ ) {
99122
var mapper = map[i-1];
@@ -103,7 +126,7 @@ angular.module('ui.bootstrap.dateparser', [])
103126
}
104127

105128
if ( isValid(fields.year, fields.month, fields.date) ) {
106-
dt = new Date( fields.year, fields.month, fields.date, fields.hours);
129+
dt = new Date(fields.year, fields.month, fields.date, fields.hours, fields.minutes, fields.seconds);
107130
}
108131

109132
return dt;

Diff for: 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)