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

FIX - DatePicker now accepts input value fields with the right formatDate #1855

Closed
wants to merge 5 commits into from
Closed
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
18 changes: 14 additions & 4 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,23 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
ngModel.$setValidity('date', true);
return viewValue;
} else if (angular.isString(viewValue)) {
var date = new Date(viewValue);
if (isNaN(date)) {
var dateStr = dateFilter(viewValue, dateFormat);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it uses the dateFilter service to format the "Date typed (string)" in the format the user is expecting.

if (dateStr === undefined) {
ngModel.$setValidity('date', false);
return undefined;
} else {
ngModel.$setValidity('date', true);
return date;
var dateParts = dateStr.split(/\/|\.|\-/); //check for "/" or "." or "-" to split date
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it split the date that is in the String format check if it was typed like 99/99/9999 or 99-99-9999 or 99.99.9999 so it can recreate it in a new date.

var date = new Date(dateStr);
if (dateParts.length === 3){
date = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
}
if (isNaN(date)) {
ngModel.$setValidity('date', false);
return undefined;
} else {
ngModel.$setValidity('date', true);
return date;
}
}
} else {
ngModel.$setValidity('date', false);
Expand Down