Skip to content

Commit

Permalink
fix(datepicker): date manipulation was jumping over a month top (#3130)
Browse files Browse the repository at this point in the history
fixes #2902
  • Loading branch information
valorkin authored Nov 28, 2017
1 parent 13bf18e commit a7416e6
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/bs-moment/utils/date-setters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TimeUnit } from '../types';
import { daysInMonth } from '../units/month';

const defaultTimeUnit: TimeUnit = {
year: 0,
Expand Down Expand Up @@ -30,11 +31,17 @@ export function createDate(

export function shiftDate(date: Date, unit: TimeUnit): Date {
const _unit = Object.assign({}, defaultTimeUnit, unit);
const year = date.getFullYear() + _unit.year;
const month = date.getMonth() + _unit.month;
let day = date.getDate() + _unit.day;
if (_unit.month && !_unit.day) {
day = Math.min(day, daysInMonth(year, month));
}

return createDate(
date.getFullYear() + _unit.year,
date.getMonth() + _unit.month,
date.getDate() + _unit.day,
year,
month,
day,
date.getHours() + _unit.hour,
date.getMinutes() + _unit.minute,
date.getSeconds() + _unit.seconds
Expand Down

0 comments on commit a7416e6

Please sign in to comment.