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

Commit dc558a8

Browse files
committed
fix: AM/PM switch on arrow down from 12 AM
- hoursIncrement and minutesIncrement not reset on 00:00
1 parent f45597c commit dc558a8

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

src/Vue3DatePicker/components/TimePicker/TimeInput.vue

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import { IDateFilter, IDefaultSelect, ITimeValue } from '../../interfaces';
7878
import { getArrayInArray, hoursToAmPmHours } from '../../utils/util';
7979
import SelectionGrid from '../SelectionGrid.vue';
80+
import { addDateHours, addDateMinutes, subDateHours, subDateMinutes } from '../../utils/date-utils';
8081
8182
const emit = defineEmits(['setHours', 'setMinutes', 'update:hours', 'update:minutes']);
8283
const props = defineProps({
@@ -175,14 +176,7 @@
175176
return;
176177
}
177178
}
178-
if (
179-
(props.is24 && hours.value + +props.hoursIncrement >= 24) ||
180-
(!props.is24 && hours.value + +props.hoursIncrement >= 12)
181-
) {
182-
emit('update:hours', 0);
183-
} else {
184-
emit('update:hours', hours.value + +props.hoursIncrement);
185-
}
179+
emit('update:hours', addDateHours(hours.value, +props.hoursIncrement));
186180
} else {
187181
if (props.minTime.hours) {
188182
if (hours.value - +props.hoursIncrement < +props.minTime.hours) {
@@ -194,11 +188,7 @@
194188
return;
195189
}
196190
}
197-
if (hours.value - +props.hoursIncrement < 0) {
198-
emit('update:hours', props.is24 ? 24 - +props.hoursIncrement : 12 - -+props.hoursIncrement);
199-
} else {
200-
emit('update:hours', hours.value - +props.hoursIncrement);
201-
}
191+
emit('update:hours', subDateHours(hours.value, +props.hoursIncrement));
202192
}
203193
};
204194
@@ -214,11 +204,7 @@
214204
return;
215205
}
216206
}
217-
if (minutes.value + +props.minutesIncrement >= 60) {
218-
emit('update:minutes', 0);
219-
} else {
220-
emit('update:minutes', minutes.value + +props.minutesIncrement);
221-
}
207+
emit('update:minutes', addDateMinutes(minutes.value, +props.minutesIncrement));
222208
} else {
223209
if (props.minTime.minutes) {
224210
if (minutes.value - +props.minutesIncrement < +props.minTime.minutes) {
@@ -230,11 +216,7 @@
230216
return;
231217
}
232218
}
233-
if (minutes.value - +props.minutesIncrement < 0) {
234-
emit('update:minutes', 60 - +props.minutesIncrement);
235-
} else {
236-
emit('update:minutes', minutes.value - +props.minutesIncrement);
237-
}
219+
emit('update:minutes', subDateMinutes(minutes.value, +props.minutesIncrement));
238220
}
239221
};
240222

src/Vue3DatePicker/utils/date-utils.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import isEqual from 'date-fns/isEqual';
1818
import getISOWeek from 'date-fns/getISOWeek';
1919
import addMonths from 'date-fns/addMonths';
2020
import addDays from 'date-fns/addDays';
21+
import addHours from 'date-fns/addHours';
22+
import subHours from 'date-fns/subHours';
23+
import addMinutes from 'date-fns/addMinutes';
24+
import subMinutes from 'date-fns/subMinutes';
2125

2226
import { IMonthValue, ITimeValue } from '../interfaces';
2327

@@ -228,3 +232,31 @@ export const getDateMinutes = (date?: Date): number => {
228232
export const getAddedDays = (date: Date, days: number): Date => {
229233
return addDays(date, days);
230234
};
235+
236+
/**
237+
* Add specific amount of hours to the previous hours
238+
*/
239+
export const addDateHours = (hours: number, toAdd: number): number => {
240+
return getHours(addHours(setHours(new Date(), hours), toAdd));
241+
};
242+
243+
/**
244+
* Subtract specific amount of hours to the previous hours
245+
*/
246+
export const subDateHours = (hours: number, toSub: number): number => {
247+
return getHours(subHours(setHours(new Date(), hours), toSub));
248+
};
249+
250+
/**
251+
* Add specific amount of minutes to the previous minutes
252+
*/
253+
export const addDateMinutes = (minutes: number, toAdd: number): number => {
254+
return getMinutes(addMinutes(setMinutes(new Date(), minutes), toAdd));
255+
};
256+
257+
/**
258+
* Subtract specific amount of minutes to the previous minutes
259+
*/
260+
export const subDateMinutes = (minutes: number, toSub: number): number => {
261+
return getMinutes(subMinutes(setMinutes(new Date(), minutes), toSub));
262+
};

0 commit comments

Comments
 (0)