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

Commit b4bf525

Browse files
committed
refactor: Show hours as 00 instead of 24 on 24 hours mode
1 parent 4d33e95 commit b4bf525

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/Vue3DatePicker/interfaces.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,8 @@ export interface TimeInputProps {
249249

250250
export type IHoursRange = [number, number];
251251
export type IMinutesRange = [number, number];
252+
253+
export interface IHoursOptions {
254+
hourCycle?: string;
255+
hour12?: boolean;
256+
}

src/Vue3DatePicker/utils/util.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FormatOptions, ICalendarDate, ICalendarDay, IDefaultSelect } from '../interfaces';
1+
import { FormatOptions, ICalendarDate, ICalendarDay, IDefaultSelect, IHoursOptions } from '../interfaces';
22

33
/**
44
* Depending on a week start get starting date of the current calendar
@@ -61,6 +61,13 @@ export const getCalendarDays = (month: number, year: number, start: number): ICa
6161
return weeks;
6262
};
6363

64+
/**
65+
* Different config for 24h and 12H
66+
*/
67+
const getHoursOptions = (is24: boolean): IHoursOptions => {
68+
return is24 ? { hourCycle: 'h23' } : { hour12: true };
69+
};
70+
6471
/**
6572
* Format Date object into readable format, specified by props
6673
*/
@@ -73,7 +80,12 @@ export const formatSingleDate = (
7380
): string => {
7481
let options = formatOptions;
7582
if (showTime) {
76-
options = Object.assign(options, { hour: 'numeric', minute: 'numeric', hour12: !is24 });
83+
options = Object.assign(options, {
84+
hour: 'numeric',
85+
minute: 'numeric',
86+
hourCycle: 'h23',
87+
...getHoursOptions(is24),
88+
});
7789
}
7890
return date.toLocaleDateString(locale, options);
7991
};
@@ -91,7 +103,7 @@ export const formatRangeDate = (
91103
): string | string[] => {
92104
let options = formatOptions;
93105
if (showTime) {
94-
options = Object.assign(options, { hour: 'numeric', minute: 'numeric', hour12: !is24 });
106+
options = Object.assign(options, { hour: 'numeric', minute: 'numeric', ...getHoursOptions(is24) });
95107
}
96108
if (returnArr) {
97109
return [dates[0].toLocaleDateString(locale, options), dates[1].toLocaleDateString(locale, options)];

0 commit comments

Comments
 (0)