Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit 795a0d2

Browse files
committed
Invalid date pickup for locale date format
If language local date format has different order like standard "English", then selection in popup calendar get invalid date. For example, Czech or Slovak date format start with day number (dd.MM.yy). If date format not start with month number, then new Date() parse invalid date or if day number is not bigger then 12, switch day and month
1 parent 130d321 commit 795a0d2

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/js/angular-datepicker.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,41 @@
276276

277277
$scope.year = Number($scope.year) + 1;
278278
}
279+
, localDateTimestamp = function localDateTimestamp(rawDate, dateFormat) {
280+
281+
var formattingTokens = /(\[[^\[]*\])|(\\)?([Hh]mm(ss)?|MM?M?M?|dd?d?|yy?yy?y?|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|kk?|mm?|ss?|S{1,9}|x|X|zz?|ZZ?|.)/g;
282+
var formatDate = dateFormat.match(formattingTokens);
283+
var dateSplit = rawDate.split(/\D/);
284+
var m;
285+
var d;
286+
var y;
287+
288+
console.log('tutu');
289+
290+
formatDate = formatDate.filter(function (item) {
291+
if (item.match(/^[a-zA-Z]+$/i) !== null) {
292+
return item;
293+
}
294+
});
295+
296+
for (var index = 0; index < formatDate.length; index++) {
297+
var element = formatDate[index];
298+
299+
if (element.indexOf('d') > -1) {
300+
d = dateSplit[index];
301+
}
302+
303+
if (element.indexOf('M') > -1) {
304+
m = dateSplit[index];
305+
}
306+
307+
if (element.indexOf('y') > -1) {
308+
y = dateSplit[index];
309+
}
310+
}
311+
312+
return new Date(y + '/' + m + '/' + d);
313+
}
279314
, setInputValue = function setInputValue() {
280315

281316
if ($scope.isSelectableMinDate($scope.year + '/' + $scope.monthNumber + '/' + $scope.day) &&
@@ -618,9 +653,8 @@
618653
thisInput[0].value.length > 0) {
619654

620655
try {
621-
622656
if (dateFormat) {
623-
date = new Date($filter('date')(thisInput[0].value.toString(), dateFormat));
657+
date = localDateTimestamp(thisInput[0].value.toString(), dateFormat);
624658
} else {
625659
date = new Date(thisInput[0].value.toString());
626660
}

0 commit comments

Comments
 (0)