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

fix(date): Apply the ui-date-format to the dateOptions if provided #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
Read only <input type="text" ui-date ng-model="aDate" readonly>
</div>

<div>
<input type="text" ui-date-format="DD, MM d, yy" ui-date ng-model="isoDate" />
</div>

<div class="field">
Required date: <input type="text" name="date2" required ui-date="" ng-model="reqDate">
Expand All @@ -39,7 +42,8 @@
<script>
angular.module('MyApp', ['ui.date'])
.controller('MyCtrl', function($scope) {
$scope.aDate = '2015-10-31';
$scope.aDate = '1992-09-14T21:00:00.000Z';
$scope.isoDate = '2015-10-13T21:00:00.000Z';
$scope.dateOptions = {
dateFormat: 'dd.mm.yy',
}
Expand Down
27 changes: 22 additions & 5 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ export default angular.module('ui.date', [])
if (value) {
if (dateFormat) {
try {
return jQuery.datepicker.formatDate(dateFormat, value);
var dateFormatted = jQuery.datepicker.formatDate(dateFormat, value)
return dateFormatted;
} catch (formatException) {
return undefined;
try {
// try as converted from an iso date
var isoDate = new Date(value);
return jQuery.datepicker.formatDate(dateFormat, isoDate)
} catch (conversionError) {
return value;
}
}
}

Expand All @@ -38,8 +45,13 @@ export default angular.module('ui.date', [])
}

if (angular.isString(valueToParse)) {
if (dateFormat) {
return jQuery.datepicker.parseDate(dateFormat, valueToParse);
if (dateFormat) {
try {
var parsedDate = jQuery.datepicker.parseDate(dateFormat, valueToParse);
return parsedDate;
} catch (formatException) {
// Keep on trying to parse as an iso string
}
}

var isoDate = new Date(valueToParse);
Expand All @@ -65,8 +77,13 @@ export default angular.module('ui.date', [])
var $element = jQuery(element);

var getOptions = function() {
return angular.extend({}, uiDateConfig, scope.$eval(attrs.uiDate));
var dateFormat = {};
if (attrs.uiDateFormat) {
dateFormat = { dateFormat: attrs.uiDateFormat }
}
return angular.extend({}, uiDateConfig, dateFormat, scope.$eval(attrs.uiDate));
};

var initDateWidget = function() {
var showing = false;
var opts = getOptions();
Expand Down
3 changes: 2 additions & 1 deletion src/date.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ describe('uiDateFormat', function() {
})
});


// it('should validate null and blank dates as valid', function() {
// inject(function($compile, $rootScope) {
// $rootScope.x = null;
Expand Down Expand Up @@ -498,7 +499,7 @@ describe('uiDateFormat', function() {
expect(function incompleteValue() {
ngModel.$setViewValue('2015-');
}).not.toThrow();
expect(ngModel.$modelValue).toBeUndefined();
expect(ngModel.$modelValue).toBe('2015-01-01');
});
});

Expand Down