Skip to content

Commit

Permalink
fix(datepicker): don't use the locale in the default formatting function
Browse files Browse the repository at this point in the history
By default the datepicker would insert the date into the view after passing it through toLocaleDateString.
This means that if the user's locale date string couldn't be parsed by `new Date`, the datepicker would have inserted an invalid date.
This change switches to always using the US date format, because it is parse-able by the Date constructor. This shouldn't be unexpected, because the documentation states that users should override the date functions if they're dealing with different kinds of date formats.

Fixes angular#7456.
  • Loading branch information
crisbeto committed Apr 21, 2016
1 parent a8f43b8 commit a8e77b8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/components/datepicker/dateLocale.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ describe('$mdDateLocale', function() {
expect(dateLocale.parseDate(dateStr)).toEqual(jasmine.any(Date));
});

it('should default to the US date formatting', function() {
var date = new Date(2014, 2, 25);
var dateStr = dateLocale.formatDate(date);
expect(dateStr).toBe('3/25/2014');
});

it('should have default date completion detection', function() {
// Valid dates.
expect(dateLocale.isDateComplete('04/05/15')).toBe(true);
Expand Down
4 changes: 2 additions & 2 deletions src/components/datepicker/dateLocaleProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
* @param $locale
* @returns {DateLocale}
*/
DateLocaleProvider.prototype.$get = function($locale) {
DateLocaleProvider.prototype.$get = function($locale, $filter) {
/**
* Default date-to-string formatting function.
* @param {!Date} date
Expand All @@ -172,7 +172,7 @@
formatDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 1, 0, 0);
}

return formatDate.toLocaleDateString();
return $filter('date')(formatDate, 'M/d/yyyy');
}

/**
Expand Down

0 comments on commit a8e77b8

Please sign in to comment.