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

Commit ea388b3

Browse files
committed
feat(dateparser): add new format support
- Adds `ww` support - Adds `w` support - Adds `G`, `GG`, `GGG` support - Adds `GGGG` support Closes #3418 Closes #4833
1 parent 8c3c38d commit ea388b3

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

src/dateparser/dateparser.js

+24
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,30 @@ angular.module('ui.bootstrap.dateparser', [])
145145
this.hours += toInt(sign + hours);
146146
this.minutes += toInt(sign + minutes);
147147
}
148+
},
149+
{
150+
key: 'ww',
151+
regex: '[0-4][0-9]|5[0-3]'
152+
},
153+
{
154+
key: 'w',
155+
regex: '[0-9]|[1-4][0-9]|5[0-3]'
156+
},
157+
{
158+
key: 'GGGG',
159+
regex: $locale.DATETIME_FORMATS.ERANAMES.join('|').replace(/\s/g, '\\s')
160+
},
161+
{
162+
key: 'GGG',
163+
regex: $locale.DATETIME_FORMATS.ERAS.join('|')
164+
},
165+
{
166+
key: 'GG',
167+
regex: $locale.DATETIME_FORMATS.ERAS.join('|')
168+
},
169+
{
170+
key: 'G',
171+
regex: $locale.DATETIME_FORMATS.ERAS.join('|')
148172
}
149173
];
150174
};

src/dateparser/docs/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ Certain format codes support i18n. Check this [guide](https://docs.angularjs.org
121121
* `Z`
122122
_(Example: `-0800`)_ -
123123
Parses the timezone offset in a signed 4 digit representation
124+
125+
* `ww`
126+
_(Example: `03`, Leading 0)_ -
127+
Parses the week number
128+
129+
* `w`
130+
_(Example: `03`)_ -
131+
Parses the week number
132+
133+
* `G`, `GG`, `GGG`
134+
_(Example: `AD`)_ -
135+
Parses the era (`AD` or `BC`)
136+
* `GGGG`
137+
_(Example: `Anno Domini`)_ -
138+
Parses the long form of the era (`Anno Domini` or `Before Christ`)
124139

125140
\* The ones marked with `Leading 0`, needs a leading 0 for values less than 10. Exception being milliseconds which needs it for values under 100.
126141

src/dateparser/test/dateparser.spec.js

+54
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,60 @@ describe('date parser', function() {
234234
expectParse('22.March.15.22:33:4 -1200', 'd.MMMM.yy.HH:mm:s Z', new Date(2015, 2, 22, 10, 33, 4));
235235
expectParse('22.March.15.22:3:4 +1500', 'd.MMMM.yy.HH:m:s Z', new Date(2015, 2, 23, 13, 3, 4));
236236
});
237+
238+
it('should work correctly for `ww`', function() {
239+
expectParse('17.November.13.45', 'd.MMMM.yy.ww', new Date(2013, 10, 17, 0));
240+
expectParse('8-March-1991-09', 'd-MMMM-yyyy-ww', new Date(1991, 2, 8, 0));
241+
expectParse('February/5/1980/05', 'MMMM/d/yyyy/ww', new Date(1980, 1, 5, 0));
242+
expectParse('1955/February/5/04', 'yyyy/MMMM/d/ww', new Date(1955, 1, 5, 0));
243+
expectParse('11-08-13 44', 'd-MM-yy ww', new Date(2013, 7, 11, 0));
244+
expectParse('0001/03/6 10', 'yyyy/MM/d ww', oldDate);
245+
});
246+
247+
it('should work correctly for `w`', function() {
248+
expectParse('17.November.13.45', 'd.MMMM.yy.w', new Date(2013, 10, 17, 0));
249+
expectParse('8-March-1991-9', 'd-MMMM-yyyy-w', new Date(1991, 2, 8, 0));
250+
expectParse('February/5/1980/5', 'MMMM/d/yyyy/w', new Date(1980, 1, 5, 0));
251+
expectParse('1955/February/5/4', 'yyyy/MMMM/d/w', new Date(1955, 1, 5, 0));
252+
expectParse('11-08-13 44', 'd-MM-yy w', new Date(2013, 7, 11, 0));
253+
expectParse('0001/03/6 10', 'yyyy/MM/d w', oldDate);
254+
});
255+
256+
it('should work correctly for `G`', function() {
257+
expectParse('17.November.13.AD', 'd.MMMM.yy.G', new Date(2013, 10, 17, 0));
258+
expectParse('8-March-1991-BC', 'd-MMMM-yyyy-G', new Date(1991, 2, 8, 0));
259+
expectParse('February/5/1980/AD', 'MMMM/d/yyyy/G', new Date(1980, 1, 5, 0));
260+
expectParse('1955/February/5/BC', 'yyyy/MMMM/d/G', new Date(1955, 1, 5, 0));
261+
expectParse('11-08-13 AD', 'd-MM-yy G', new Date(2013, 7, 11, 0));
262+
expectParse('0001/03/6 BC', 'yyyy/MM/d G', oldDate);
263+
});
264+
265+
it('should work correctly for `GG`', function() {
266+
expectParse('17.November.13.AD', 'd.MMMM.yy.GG', new Date(2013, 10, 17, 0));
267+
expectParse('8-March-1991-BC', 'd-MMMM-yyyy-GG', new Date(1991, 2, 8, 0));
268+
expectParse('February/5/1980/AD', 'MMMM/d/yyyy/GG', new Date(1980, 1, 5, 0));
269+
expectParse('1955/February/5/BC', 'yyyy/MMMM/d/GG', new Date(1955, 1, 5, 0));
270+
expectParse('11-08-13 AD', 'd-MM-yy GG', new Date(2013, 7, 11, 0));
271+
expectParse('0001/03/6 BC', 'yyyy/MM/d GG', oldDate);
272+
});
273+
274+
it('should work correctly for `GGG`', function() {
275+
expectParse('17.November.13.AD', 'd.MMMM.yy.GGG', new Date(2013, 10, 17, 0));
276+
expectParse('8-March-1991-BC', 'd-MMMM-yyyy-GGG', new Date(1991, 2, 8, 0));
277+
expectParse('February/5/1980/AD', 'MMMM/d/yyyy/GGG', new Date(1980, 1, 5, 0));
278+
expectParse('1955/February/5/BC', 'yyyy/MMMM/d/GGG', new Date(1955, 1, 5, 0));
279+
expectParse('11-08-13 AD', 'd-MM-yy GGG', new Date(2013, 7, 11, 0));
280+
expectParse('0001/03/6 BC', 'yyyy/MM/d GGG', oldDate);
281+
});
282+
283+
it('should work correctly for `GGGG`', function() {
284+
expectParse('17.November.13.Anno Domini', 'd.MMMM.yy.GGGG', new Date(2013, 10, 17, 0));
285+
expectParse('8-March-1991-Before Christ', 'd-MMMM-yyyy-GGGG', new Date(1991, 2, 8, 0));
286+
expectParse('February/5/1980/Anno Domini', 'MMMM/d/yyyy/GGGG', new Date(1980, 1, 5, 0));
287+
expectParse('1955/February/5/Before Christ', 'yyyy/MMMM/d/GGGG', new Date(1955, 1, 5, 0));
288+
expectParse('11-08-13 Anno Domini', 'd-MM-yy GGGG', new Date(2013, 7, 11, 0));
289+
expectParse('0001/03/6 Before Christ', 'yyyy/MM/d GGGG', oldDate);
290+
});
237291
});
238292

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

0 commit comments

Comments
 (0)