Skip to content

Commit a7416e6

Browse files
authored
fix(datepicker): date manipulation was jumping over a month top (#3130)
fixes #2902
1 parent 13bf18e commit a7416e6

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/bs-moment/utils/date-setters.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TimeUnit } from '../types';
2+
import { daysInMonth } from '../units/month';
23

34
const defaultTimeUnit: TimeUnit = {
45
year: 0,
@@ -30,11 +31,17 @@ export function createDate(
3031

3132
export function shiftDate(date: Date, unit: TimeUnit): Date {
3233
const _unit = Object.assign({}, defaultTimeUnit, unit);
34+
const year = date.getFullYear() + _unit.year;
35+
const month = date.getMonth() + _unit.month;
36+
let day = date.getDate() + _unit.day;
37+
if (_unit.month && !_unit.day) {
38+
day = Math.min(day, daysInMonth(year, month));
39+
}
3340

3441
return createDate(
35-
date.getFullYear() + _unit.year,
36-
date.getMonth() + _unit.month,
37-
date.getDate() + _unit.day,
42+
year,
43+
month,
44+
day,
3845
date.getHours() + _unit.hour,
3946
date.getMinutes() + _unit.minute,
4047
date.getSeconds() + _unit.seconds

0 commit comments

Comments
 (0)