From 82cb637dd12288d9fa0d42783f672fc71b94ffda Mon Sep 17 00:00:00 2001 From: Chris Chua Date: Mon, 13 Apr 2015 22:53:20 -0700 Subject: [PATCH] feat(dateparser): add support for milliseconds Closes #3537 --- src/dateparser/dateparser.js | 8 ++++++-- src/dateparser/test/dateparser.spec.js | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/dateparser/dateparser.js b/src/dateparser/dateparser.js index 1b0e8135a3..fa3004533b 100644 --- a/src/dateparser/dateparser.js +++ b/src/dateparser/dateparser.js @@ -65,6 +65,10 @@ angular.module('ui.bootstrap.dateparser', []) regex: '[0-9]|[1-5][0-9]', apply: function(value) { this.minutes = +value; } }, + 'sss': { + regex: '[0-9][0-9][0-9]', + apply: function(value) { this.milliseconds = +value; } + }, 'ss': { regex: '[0-5][0-9]', apply: function(value) { this.seconds = +value; } @@ -120,7 +124,7 @@ angular.module('ui.bootstrap.dateparser', []) results = input.match(regex); if ( results && results.length ) { - var fields = { year: 1900, month: 0, date: 1, hours: 0, minutes: 0, seconds: 0 }, dt; + var fields = { year: 1900, month: 0, date: 1, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }, dt; for( var i = 1, n = results.length; i < n; i++ ) { var mapper = map[i-1]; @@ -130,7 +134,7 @@ angular.module('ui.bootstrap.dateparser', []) } if ( isValid(fields.year, fields.month, fields.date) ) { - dt = new Date(fields.year, fields.month, fields.date, fields.hours, fields.minutes, fields.seconds); + dt = new Date(fields.year, fields.month, fields.date, fields.hours, fields.minutes, fields.seconds, fields.milliseconds); } return dt; diff --git a/src/dateparser/test/dateparser.spec.js b/src/dateparser/test/dateparser.spec.js index 1314826aa4..7ed6bb0e58 100644 --- a/src/dateparser/test/dateparser.spec.js +++ b/src/dateparser/test/dateparser.spec.js @@ -96,6 +96,16 @@ describe('date parser', function () { expectParse('22.March.15.2:1', 'd.MMMM.yy.H:m', new Date(2015, 2, 22, 2, 1)); }); + it('should work correctly for `sss`', function() { + expectParse('22.March.15.123', 'd.MMMM.yy.sss', new Date(2015, 2, 22, 0, 0, 0, 123)); + expectParse('8-March-1991-059', 'd-MMMM-yyyy-sss', new Date(1991, 2, 8, 0, 0, 0, 59)); + expectParse('February/5/1980/000', 'MMMM/d/yyyy/sss', new Date(1980, 1, 5, 0, 0, 0)); + expectParse('1955/February/5 003', 'yyyy/MMMM/d sss', new Date(1955, 1, 5, 0, 0, 0, 3)); + expectParse('11-08-13 046', 'd-MM-yy sss', new Date(2013, 7, 11, 0, 0, 0, 46)); + expectParse('22.March.15.22:33:044', 'd.MMMM.yy.HH:mm:sss', new Date(2015, 2, 22, 22, 33, 0, 44)); + expectParse('22.March.15.0:0:001', 'd.MMMM.yy.H:m:sss', new Date(2015, 2, 22, 0, 0, 0, 1)); + }); + it('should work correctly for `ss`', function() { expectParse('22.March.15.22', 'd.MMMM.yy.ss', new Date(2015, 2, 22, 0, 0, 22)); expectParse('8-March-1991-59', 'd-MMMM-yyyy-ss', new Date(1991, 2, 8, 0, 0, 59));