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

fix(datepicker): fix usage of non-standard format #5188

Closed
Closed
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,30 @@ describe('datepicker', function() {
});
});

describe('custom format with optional leading zeroes', function() {
beforeEach(inject(function() {
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup="d!-M!-yyyy" is-open="true"><div>')($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';
Expand Down