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

Commit 788a269

Browse files
committed
Local date formated parse to Date object
Cleanup code.
1 parent 9703f8a commit 788a269

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

src/js/angular-datepicker.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -278,45 +278,55 @@
278278
}
279279
, localDateTimestamp = function localDateTimestamp(rawDate, dateFormatDefinition) {
280280

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-
,formatDate = dateFormatDefinition.match(formattingTokens)
283-
,dateSplit, m, d, y, index, el;
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;
284283

285284
for (index = 0; index < datetime.MONTH.length; index += 1) {
286-
el = datetime.MONTH[index];
287-
if (rawDate.indexOf(el) > -1) {
288-
rawDate = rawDate.replace(el, 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);
289290
break;
290291
}
292+
293+
if (rawDate.indexOf(shortName) !== -1) {
294+
rawDate = rawDate.replace(shortName, index + 1);
295+
break;
296+
}
291297
}
292298

293-
dateSplit = rawDate.split(/\D/);
299+
dateSplit = rawDate
300+
.split(/\D/)
301+
.filter(function dateSplitFilter(item) {
302+
return item.length > 0;
303+
});
294304

295-
dateSplit = dateSplit.filter(function dateSplitFilter(item) {
296-
if (item.length > 0) {
297-
return item;
298-
}
299-
});
300-
301-
formatDate = formatDate.filter(function fromatDateFilter(item) {
302-
if (item.match(/^[a-zA-Z]+$/i) !== null) {
303-
return item;
304-
}
305-
});
305+
formatDate = dateFormatDefinition
306+
.match(formattingTokens)
307+
.filter(function fromatDateFilter(item) {
308+
return item.match(/^[a-zA-Z]+$/i) !== null;
309+
});
306310

307311
for (index = 0; index < formatDate.length; index += 1) {
308312
el = formatDate[index];
309313

310-
if (el.indexOf('d') > -1) {
311-
d = dateSplit[index];
312-
}
313-
314-
if (el.indexOf('M') > -1) {
315-
m = dateSplit[index];
316-
}
317-
318-
if (el.indexOf('y') > -1) {
319-
y = dateSplit[index];
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+
}
320330
}
321331
}
322332

0 commit comments

Comments
 (0)