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

Commit 9f8e62f

Browse files
authored
Merge pull request #243 from bender404/master
Invalid date pickup for locale date format
2 parents 48bfcb9 + 788a269 commit 9f8e62f

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

src/js/angular-datepicker.js

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

277277
$scope.year = Number($scope.year) + 1;
278278
}
279+
, localDateTimestamp = function localDateTimestamp(rawDate, dateFormatDefinition) {
280+
281+
var formattingTokens = /(\[[^\[]*\])|(\\)?([Hh]mm(ss)?|MMMM|MMM|MM|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+
,formatDate,dateSplit, m, d, y, index, el, longName, shortName;
283+
284+
for (index = 0; index < datetime.MONTH.length; index += 1) {
285+
longName = datetime.MONTH[index];
286+
shortName = datetime.SHORTMONTH[index];
287+
288+
if (rawDate.indexOf(longName) !== -1) {
289+
rawDate = rawDate.replace(longName, index + 1);
290+
break;
291+
}
292+
293+
if (rawDate.indexOf(shortName) !== -1) {
294+
rawDate = rawDate.replace(shortName, index + 1);
295+
break;
296+
}
297+
}
298+
299+
dateSplit = rawDate
300+
.split(/\D/)
301+
.filter(function dateSplitFilter(item) {
302+
return item.length > 0;
303+
});
304+
305+
formatDate = dateFormatDefinition
306+
.match(formattingTokens)
307+
.filter(function fromatDateFilter(item) {
308+
return item.match(/^[a-zA-Z]+$/i) !== null;
309+
});
310+
311+
for (index = 0; index < formatDate.length; index += 1) {
312+
el = formatDate[index];
313+
314+
switch (true) {
315+
case el.indexOf('d') !== -1: {
316+
d = dateSplit[index];
317+
break;
318+
}
319+
case el.indexOf('M') !== -1: {
320+
m = dateSplit[index];
321+
break;
322+
}
323+
case el.indexOf('y') !== -1: {
324+
y = dateSplit[index];
325+
break;
326+
}
327+
default: {
328+
break;
329+
}
330+
}
331+
}
332+
333+
return new Date(y + '/' + m + '/' + d);
334+
}
279335
, setInputValue = function setInputValue() {
280336

281337
if ($scope.isSelectableMinDate($scope.year + '/' + $scope.monthNumber + '/' + $scope.day) &&
@@ -619,9 +675,8 @@
619675
thisInput[0].value.length > 0) {
620676

621677
try {
622-
623678
if (dateFormat) {
624-
date = new Date($filter('date')(thisInput[0].value.toString(), dateFormat));
679+
date = localDateTimestamp(thisInput[0].value.toString(), dateFormat);
625680
} else {
626681
date = new Date(thisInput[0].value.toString());
627682
}

0 commit comments

Comments
 (0)