From b1cfc5762dd80d8eecec95385a4dcf335cdb8ecd Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Mon, 2 Nov 2015 14:51:18 -0800 Subject: [PATCH] feat(dateparser): add M! and d! support - Add M! and d! for optional support of leading zeroes in date string format Closes #4805 Closes #4809 --- src/dateparser/dateparser.js | 8 +++++++ src/dateparser/docs/README.md | 10 ++++++++- src/dateparser/test/dateparser.spec.js | 30 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/dateparser/dateparser.js b/src/dateparser/dateparser.js index b53d97c646..26a1397e6f 100644 --- a/src/dateparser/dateparser.js +++ b/src/dateparser/dateparser.js @@ -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; } diff --git a/src/dateparser/docs/README.md b/src/dateparser/docs/README.md index be2f5d5d10..414eace070 100644 --- a/src/dateparser/docs/README.md +++ b/src/dateparser/docs/README.md @@ -53,7 +53,11 @@ 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. @@ -61,6 +65,10 @@ Certain format codes support i18n. Check this [guide](https://docs.angularjs.org * `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)_ - diff --git a/src/dateparser/test/dateparser.spec.js b/src/dateparser/test/dateparser.spec.js index 2773a20f2a..6803116339 100644 --- a/src/dateparser/test/dateparser.spec.js +++ b/src/dateparser/test/dateparser.spec.js @@ -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));