Skip to content

Commit 0e0f5d9

Browse files
committed
fix($formatters): Use model format first when parsing strings stored in the model.
The use the model format first (rather than input format) when parsing and formatting dates stored as strings in the model. This allows the model to contain 09/01/2016 (MM/DD/YYYY) and display as the more rational 01/09/2016 (DD/MM/YYYY) fixes #22
1 parent 79e29ab commit 0e0f5d9

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/dateTimeInput.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
var modelType = (attrs.modelType || 'Date')
113113

114114
var inputFormats = [attrs.dateTimeInput, modelType].concat(scope.dateFormats).concat([moment.ISO_8601]).filter(unique)
115+
var formatterFormats = [modelType].concat(inputFormats).filter(unique)
115116

116117
// Behaviors
117118
controller.$parsers.unshift(dateTimeParserFactory(modelType, inputFormats, dateParseStrict))
@@ -146,7 +147,7 @@
146147
} else if (angular.isNumber(modelValue)) {
147148
return moment.utc(modelValue).format(displayFormat)
148149
}
149-
return moment(modelValue, inputFormats, moment.locale(), dateParseStrict).format(displayFormat)
150+
return moment(modelValue, formatterFormats, moment.locale(), dateParseStrict).format(displayFormat)
150151
}
151152

152153
function applyFormatters () {

test/en/modelType.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,15 @@ describe('modelType', function () {
340340
expect(element.val()).toBe('Invalid date')
341341
})
342342

343+
it('model and display format can be different and ambiguous', function () {
344+
$rootScope.date = '11/12/2016'
345+
346+
var element = $compile('<input data-date-time-input="DD/MM/YYYY" data-ng-model="date" data-model-type="MM/DD/YYYY">')($rootScope)
347+
$rootScope.$digest()
348+
349+
expect(element.val()).toBe('12/11/2016')
350+
})
351+
343352
it('is valid if user deletes input', function () {
344353
var element = $compile('<input data-date-time-input="M/D/YYYY" data-ng-model="date" data-model-type="YYYY-MM-DD">')($rootScope)
345354
$rootScope.$digest()

0 commit comments

Comments
 (0)