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

Commit

Permalink
feat(dateparser): add M! and d! support
Browse files Browse the repository at this point in the history
- Add M! and d! for optional support of leading zeroes in date string format

Closes #4805
Closes #4809
  • Loading branch information
wesleycho committed Nov 2, 2015
1 parent 3f5b420 commit b1cfc57
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/dateparser/dateparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ angular.module('ui.bootstrap.dateparser', [])
regex: '\\d{1,4}',
apply: function(value) { this.year = +value; }
},
'M!': {
regex: '0?[1-9]|1[0-2]',
apply: function(value) { this.month = value - 1; }
},
'MMMM': {
regex: $locale.DATETIME_FORMATS.MONTH.join('|'),
apply: function(value) { this.month = $locale.DATETIME_FORMATS.MONTH.indexOf(value); }
Expand All @@ -41,6 +45,10 @@ angular.module('ui.bootstrap.dateparser', [])
regex: '[1-9]|1[0-2]',
apply: function(value) { this.month = value - 1; }
},
'd!': {
regex: '[0-2]?[0-9]{1}|3[0-1]{1}',
apply: function(value) { this.date = +value; }
},
'dd': {
regex: '[0-2][0-9]{1}|3[0-1]{1}',
apply: function(value) { this.date = +value; }
Expand Down
10 changes: 9 additions & 1 deletion src/dateparser/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,22 @@ Certain format codes support i18n. Check this [guide](https://docs.angularjs.org
* `M`
_(Example: `3`)_ -
Parses a numeric month.


* `M!`
_(Example: `3` or `03`)_ -
Parses a numeric month, but allowing an optional leading zero

* `dd`
_(Example: `05`, Leading 0)_ -
Parses a numeric day.

* `d`
_(Example: `5`)_ -
Parses a numeric day.

* `d!`
_(Example: `3` or `03`)_ -
Parses a numeric day, but allowing an optional leading zero

* `EEEE`
_(Example: `Sunday`, i18n support)_ -
Expand Down
30 changes: 30 additions & 0 deletions src/dateparser/test/dateparser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ describe('date parser', function() {
expectParse('02-5-11', 'dd-M-yy', new Date(2011, 4, 2, 0));
});

it('should work correctly for `M!`', function() {
expectParse('8/11/2013', 'M!/dd/yyyy', new Date(2013, 7, 11, 0));
expectParse('07.11.05', 'dd.M!.yy', new Date(2005, 10, 7, 0));
expectParse('02-5-11', 'dd-M!-yy', new Date(2011, 4, 2, 0));
expectParse('2/05/1980', 'M!/dd/yyyy', new Date(1980, 1, 5, 0));
expectParse('1955/2/05', 'yyyy/M!/dd', new Date(1955, 1, 5, 0));
expectParse('02-5-11', 'dd-M!-yy', new Date(2011, 4, 2, 0));

expectParse('08/11/2013', 'M!/dd/yyyy', new Date(2013, 7, 11, 0));
expectParse('07.11.05', 'dd.M!.yy', new Date(2005, 10, 7, 0));
expectParse('02-05-11', 'dd-M!-yy', new Date(2011, 4, 2, 0));
expectParse('02/05/1980', 'M!/dd/yyyy', new Date(1980, 1, 5, 0));
expectParse('1955/02/05', 'yyyy/M!/dd', new Date(1955, 1, 5, 0));
expectParse('02-05-11', 'dd-M!-yy', new Date(2011, 4, 2, 0));
});

it('should work correctly for `d`', function() {
expectParse('17.November.13', 'd.MMMM.yy', new Date(2013, 10, 17, 0));
expectParse('8-March-1991', 'd-MMMM-yyyy', new Date(1991, 2, 8, 0));
Expand All @@ -74,6 +90,20 @@ describe('date parser', function() {
expectParse('11-08-13', 'd-MM-yy', new Date(2013, 7, 11, 0));
});

it('should work correctly for `d!`', function() {
expectParse('17.November.13', 'd!.MMMM.yy', new Date(2013, 10, 17, 0));
expectParse('8-March-1991', 'd!-MMMM-yyyy', new Date(1991, 2, 8, 0));
expectParse('February/5/1980', 'MMMM/d!/yyyy', new Date(1980, 1, 5, 0));
expectParse('1955/February/5', 'yyyy/MMMM/d!', new Date(1955, 1, 5, 0));
expectParse('11-08-13', 'd!-MM-yy', new Date(2013, 7, 11, 0));

expectParse('17.November.13', 'd!.MMMM.yy', new Date(2013, 10, 17, 0));
expectParse('08-March-1991', 'd!-MMMM-yyyy', new Date(1991, 2, 8, 0));
expectParse('February/05/1980', 'MMMM/d!/yyyy', new Date(1980, 1, 5, 0));
expectParse('1955/February/05', 'yyyy/MMMM/d!', new Date(1955, 1, 5, 0));
expectParse('11-08-13', 'd!-MM-yy', new Date(2013, 7, 11, 0));
});

it('should work correctly for `EEEE`', function() {
expectParse('Sunday.17.November.13', 'EEEE.d.MMMM.yy', new Date(2013, 10, 17, 0));
expectParse('8-Friday-March-1991', 'd-EEEE-MMMM-yyyy', new Date(1991, 2, 8, 0));
Expand Down

0 comments on commit b1cfc57

Please sign in to comment.