Skip to content
This repository has been archived by the owner on Aug 20, 2022. It is now read-only.

Commit

Permalink
fix: ignore hours, minutes, seconds and milliseconds
Browse files Browse the repository at this point in the history
Fixes #192
  • Loading branch information
fengyuanchen committed Dec 20, 2018
1 parent d9bf74a commit 7afb925
Showing 1 changed file with 38 additions and 39 deletions.
77 changes: 38 additions & 39 deletions src/js/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,50 +268,49 @@ export default {
const { format } = this;
let parts = [];

if (isDate(date)) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}

if (isString(date)) {
parts = date.match(REGEXP_DIGITS) || [];
}

let parsedDate = date ? new Date(date) : new Date();

if (!isDate(parsedDate)) {
parsedDate = new Date();
}

if (parts.length === format.parts.length) {
$.each(parts, (i, part) => {
const value = parseInt(part, 10);

switch (format.parts[i]) {
case 'dd':
case 'd':
parsedDate.setDate(value);
break;

case 'mm':
case 'm':
parsedDate.setMonth(value - 1);
break;
if (!isDate(date)) {
if (isString(date)) {
parts = date.match(REGEXP_DIGITS) || [];
}

case 'yy':
parsedDate.setFullYear(2000 + value);
break;
date = date ? new Date(date) : new Date();

case 'yyyy':
// Converts 2-digit year to 2000+
parsedDate.setFullYear(part.length === 2 ? 2000 + value : value);
break;
if (!isDate(date)) {
date = new Date();
}

default:
}
});
if (parts.length === format.parts.length) {
$.each(parts, (i, part) => {
const value = parseInt(part, 10);

switch (format.parts[i]) {
case 'dd':
case 'd':
date.setDate(value);
break;

case 'mm':
case 'm':
date.setMonth(value - 1);
break;

case 'yy':
date.setFullYear(2000 + value);
break;

case 'yyyy':
// Converts 2-digit year to 2000+
date.setFullYear(part.length === 2 ? 2000 + value : value);
break;

default:
}
});
}
}

return parsedDate;
// Ignore hours, minutes, seconds and milliseconds to avoid side effect (#192)
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
},

/**
Expand Down

0 comments on commit 7afb925

Please sign in to comment.