From 428beaff4c9d9dde1e6d3c0851e09597e9254ff6 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Sat, 9 Jan 2016 06:14:39 -0800 Subject: [PATCH] fix(datepicker): fix usage of non-standard format - Fix display of model in input when using a non-standard format with the dateparser (i.e. `M!` and `d!`) Closes #5188 Fixes #5187 --- src/datepicker/datepicker.js | 5 ++++- src/datepicker/test/datepicker.spec.js | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/datepicker/datepicker.js b/src/datepicker/datepicker.js index ef3a5c4b52..4204d95dd3 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -145,7 +145,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst model = dateParser.fromTimezone(model, ngModelOptions.timezone); var dt = { date: date, - label: dateFilter(date, format), + label: dateFilter(date, format.replace(/d!/, 'dd')).replace(/M!/, 'MM'), selected: model && this.compare(date, model) === 0, disabled: this.isDisabled(date), current: this.compare(date, new Date()) === 0, @@ -729,6 +729,9 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi return value; } scope.date = dateParser.fromTimezone(value, ngModelOptions.timezone); + dateFormat = dateFormat.replace(/M!/, 'MM') + .replace(/d!/, 'dd'); + return dateFilter(scope.date, dateFormat); }); } else { diff --git a/src/datepicker/test/datepicker.spec.js b/src/datepicker/test/datepicker.spec.js index 44376dae1f..11eb8c521b 100644 --- a/src/datepicker/test/datepicker.spec.js +++ b/src/datepicker/test/datepicker.spec.js @@ -1948,6 +1948,30 @@ describe('datepicker', function() { }); }); + describe('custom format with optional leading zeroes', function() { + beforeEach(inject(function() { + var wrapElement = $compile('
')($rootScope); + $rootScope.$digest(); + assignElements(wrapElement); + })); + + it('to display the correct value in input', function() { + expect(inputEl.val()).toBe('30-09-2010'); + }); + + it('updates the input when a day is clicked', function() { + clickOption(10); + expect(inputEl.val()).toBe('08-09-2010'); + expect($rootScope.date).toEqual(new Date('September 8, 2010 15:30:00')); + }); + + it('updates the input correctly when model changes', function() { + $rootScope.date = new Date('December 25, 1983 10:00:00'); + $rootScope.$digest(); + expect(inputEl.val()).toBe('25-12-1983'); + }); + }); + describe('dynamic custom format', function() { beforeEach(inject(function() { $rootScope.format = 'dd-MMMM-yyyy';