Skip to content

Commit 1ca0da0

Browse files
committed
fix(date): Apply the ui-date-format to the dateOptions if provided
Fixes angular-ui#120
1 parent 427f864 commit 1ca0da0

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

demo/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
Read only <input type="text" ui-date ng-model="aDate" readonly>
2222
</div>
2323

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

2528
<div class="field">
2629
Required date: <input type="text" name="date2" required ui-date="" ng-model="reqDate">
@@ -39,7 +42,8 @@
3942
<script>
4043
angular.module('MyApp', ['ui.date'])
4144
.controller('MyCtrl', function($scope) {
42-
$scope.aDate = '2015-10-31';
45+
$scope.aDate = '1992-09-14T21:00:00.000Z';
46+
$scope.isoDate = '2015-10-13T21:00:00.000Z';
4347
$scope.dateOptions = {
4448
dateFormat: 'dd.mm.yy',
4549
}

src/date.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ export default angular.module('ui.date', [])
1616
if (value) {
1717
if (dateFormat) {
1818
try {
19-
return jQuery.datepicker.formatDate(dateFormat, value);
19+
var dateFormatted = jQuery.datepicker.formatDate(dateFormat, value)
20+
return dateFormatted;
2021
} catch (formatException) {
21-
return undefined;
22+
try {
23+
// try as converted from an iso date
24+
var isoDate = new Date(value);
25+
return jQuery.datepicker.formatDate(dateFormat, isoDate)
26+
} catch (conversionError) {
27+
return value;
28+
}
2229
}
2330
}
2431

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

4047
if (angular.isString(valueToParse)) {
41-
if (dateFormat) {
42-
return jQuery.datepicker.parseDate(dateFormat, valueToParse);
48+
if (dateFormat) {
49+
try {
50+
var parsedDate = jQuery.datepicker.parseDate(dateFormat, valueToParse);
51+
return parsedDate;
52+
} catch (formatException) {
53+
// Keep on trying to parse as an iso string
54+
}
4355
}
4456

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

6779
var getOptions = function() {
68-
return angular.extend({}, uiDateConfig, scope.$eval(attrs.uiDate));
80+
var dateFormat = {};
81+
if (attrs.uiDateFormat) {
82+
dateFormat = { dateFormat: attrs.uiDateFormat }
83+
}
84+
return angular.extend({}, uiDateConfig, dateFormat, scope.$eval(attrs.uiDate));
6985
};
86+
7087
var initDateWidget = function() {
7188
var showing = false;
7289
var opts = getOptions();

src/date.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ describe('uiDateFormat', function() {
462462
})
463463
});
464464

465+
465466
// it('should validate null and blank dates as valid', function() {
466467
// inject(function($compile, $rootScope) {
467468
// $rootScope.x = null;
@@ -498,7 +499,7 @@ describe('uiDateFormat', function() {
498499
expect(function incompleteValue() {
499500
ngModel.$setViewValue('2015-');
500501
}).not.toThrow();
501-
expect(ngModel.$modelValue).toBeUndefined();
502+
expect(ngModel.$modelValue).toBe('2015-01-01');
502503
});
503504
});
504505

0 commit comments

Comments
 (0)