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

Commit

Permalink
fix: set date parts in the right order
Browse files Browse the repository at this point in the history
Fixed #222
  • Loading branch information
fengyuanchen committed Sep 21, 2019
1 parent 8852e89 commit 0fe5e6c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## next

- Fix the issue of converting `31/10/2019` to `01/10/2019` when the current month only has 30 days (#222).

## 1.0.8 (Jun 23, 2019)

- Fix the issue of unable to pick the current month or year again (#203).
Expand Down
28 changes: 19 additions & 9 deletions src/js/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,27 +291,37 @@ export default {
}

if (parts.length === format.parts.length) {
// Set year and month first
$.each(parts, (i, part) => {
const value = parseInt(part, 10);

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

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

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

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

case 'yyyy':
// Converts 2-digit year to 2000+
date.setFullYear(part.length === 2 ? 2000 + value : value);
// Set day in the last to avoid converting `31/10/2019` to `01/10/2019`
$.each(parts, (i, part) => {
const value = parseInt(part, 10);

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

default:
Expand Down

0 comments on commit 0fe5e6c

Please sign in to comment.