From a0b7ee64040a2caea2ca0c39774f63b43fdcfe70 Mon Sep 17 00:00:00 2001 From: Kai Johnson Date: Wed, 5 Aug 2015 12:59:03 -0400 Subject: [PATCH] fix(dateparser): Support 12-hour format. --- src/dateparser/dateparser.js | 16 ++++++++++++++++ src/dateparser/test/dateparser.spec.js | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/dateparser/dateparser.js b/src/dateparser/dateparser.js index 9a44c3bb47..66f11ff5c3 100644 --- a/src/dateparser/dateparser.js +++ b/src/dateparser/dateparser.js @@ -53,6 +53,10 @@ angular.module('ui.bootstrap.dateparser', []) regex: '(?:0|1)[0-9]|2[0-3]', apply: function(value) { this.hours = +value; } }, + 'hh': { + regex: '0[0-9]|1[0-2]', + apply: function(value) { this.hours = +value; } + }, 'H': { regex: '1?[0-9]|2[0-3]', apply: function(value) { this.hours = +value; } @@ -76,6 +80,18 @@ angular.module('ui.bootstrap.dateparser', []) 's': { regex: '[0-9]|[1-5][0-9]', apply: function(value) { this.seconds = +value; } + }, + 'a': { + regex: $locale.DATETIME_FORMATS.AMPMS.join('|'), + apply: function(value) { + if (this.hours === 12) { + this.hours = 0; + } + + if (value === 'PM') { + this.hours += 12; + } + } } }; diff --git a/src/dateparser/test/dateparser.spec.js b/src/dateparser/test/dateparser.spec.js index 37c2bc972a..5c10a1df29 100644 --- a/src/dateparser/test/dateparser.spec.js +++ b/src/dateparser/test/dateparser.spec.js @@ -68,6 +68,16 @@ describe('date parser', function () { expectParse('11-08-13 23', 'd-MM-yy HH', new Date(2013, 7, 11, 23)); }); + it('should work correctly for `hh`', function() { + expectParse('22.March.15.22', 'd.MMMM.yy.hh', undefined); + expectParse('22.March.15.12', 'd.MMMM.yy.hh', new Date(2015, 2, 22, 12)); + expectParse('8-March-1991-11', 'd-MMMM-yyyy-HH', new Date(1991, 2, 8, 11)); + expectParse('February/5/1980/00', 'MMMM/d/yyyy/hh', new Date(1980, 1, 5, 0)); + expectParse('1955/February/5 03', 'yyyy/MMMM/d hh', new Date(1955, 1, 5, 3)); + expectParse('11-08-13 23', 'd-MM-yy hh', undefined); + expectParse('11-08-13 09', 'd-MM-yy hh', new Date(2013, 7, 11, 9)); + }); + it('should work correctly for `H`', function() { expectParse('22.March.15.22', 'd.MMMM.yy.H', new Date(2015, 2, 22, 22)); expectParse('8-March-1991-11', 'd-MMMM-yyyy-H', new Date(1991, 2, 8, 11)); @@ -125,6 +135,19 @@ describe('date parser', function () { expectParse('22.March.15.22:33:4', 'd.MMMM.yy.HH:mm:s', new Date(2015, 2, 22, 22, 33, 4)); expectParse('22.March.15.22:3:4', 'd.MMMM.yy.HH:m:s', new Date(2015, 2, 22, 22, 3, 4)); }); + + it('should work correctly for `a`', function() { + expectParse('22.March.15.10AM', 'd.MMMM.yy.hha', new Date(2015, 2, 22, 10)); + expectParse('22.March.15.10PM', 'd.MMMM.yy.hha', new Date(2015, 2, 22, 22)); + expectParse('8-March-1991-11AM', 'd-MMMM-yyyy-hha', new Date(1991, 2, 8, 11)); + expectParse('8-March-1991-11PM', 'd-MMMM-yyyy-hha', new Date(1991, 2, 8, 23)); + expectParse('February/5/1980/12AM', 'MMMM/d/yyyy/hha', new Date(1980, 1, 5, 0)); + expectParse('February/5/1980/12PM', 'MMMM/d/yyyy/hha', new Date(1980, 1, 5, 12)); + expectParse('1955/February/5 03AM', 'yyyy/MMMM/d hha', new Date(1955, 1, 5, 3)); + expectParse('1955/February/5 03PM', 'yyyy/MMMM/d hha', new Date(1955, 1, 5, 15)); + expectParse('11-08-13 09AM', 'd-MM-yy hha', new Date(2013, 7, 11, 9)); + expectParse('11-08-13 09PM', 'd-MM-yy hha', new Date(2013, 7, 11, 21)); + }); }); describe('with predefined formats', function() {