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

feat(dateparser): add M! and d! support #4809

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/dateparser/dateparser.js
Original file line number Diff line number Diff line change
@@ -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); }
@@ -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; }
10 changes: 9 additions & 1 deletion src/dateparser/docs/README.md
Original file line number Diff line number Diff line change
@@ -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)_ -
30 changes: 30 additions & 0 deletions src/dateparser/test/dateparser.spec.js
Original file line number Diff line number Diff line change
@@ -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));
@@ -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));