Fix: time format issue in datepicker mask (#16922)

Fixes: #16872 

## Summary

Fixes the DateTimePicker to respect user's 12H/24H time format
preference. Previously, the time display at the top of the
DateTimePicker always showed 24-hour format (e.g., "14:30") regardless
of the user's time format setting.

## Screenshots

_After:_ Time respects user preference, showing 12H with AM/PM (e.g.,
"03:28 PM") or 24H format
<img width="357" height="491" alt="image"
src="https://github.com/user-attachments/assets/4a03f195-a52b-4f74-84ba-c5eb2fc6c8c1"
/>


## Implementation Details

### Changes Made:

1. **TimeMask.ts** - Added `getTimeMask()` to return appropriate mask
pattern based on time format
2. **TimeBlocks.ts** - Restored as constant file per linting
requirements
3. **getTimeBlocks.ts** (new) - Dynamic block generator supporting 12H
(1-12 + AM/PM) and 24H (0-23)
4. **DateTimeBlocks.ts** - Simplified to static constant
5. **getDateTimeMask.ts** - Updated to accept and use `timeFormat`
parameter
6. **DateTimePickerInput.tsx** - Dynamically generates mask and blocks
based on user's time format preference, increased input width to
accommodate AM/PM
7. **useParseJSDateToIMaskDateTimeInputString.ts** - Formats dates with
correct time pattern
8. **useParseDateTimeInputStringToJSDate.ts** - Parses dates with
correct time pattern
9. **date-utils.ts** - Added `getTimePattern()` helper (returns 'hh:mm
a' for 12H, 'HH:mm' for 24H)
10. **parseDateTimeToString.ts** - Added optional `timeFormat` parameter
for consistency

### Technical Approach:

- Leverages existing `useDateTimeFormat()` hook to get user's
`timeFormat` preference
- Supports `TimeFormat.SYSTEM`, `TimeFormat.HOUR_12`, and
`TimeFormat.HOUR_24`
- Uses IMask blocks with appropriate ranges: 1-12 for 12H, 0-23 for 24H
- Adds 'aa' (AM/PM) block for 12-hour format with enum validation

---------

Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
Vasu Singh
2026-01-28 23:13:48 +05:30
committed by GitHub
parent 9672ca09a2
commit 59f7582463
10 changed files with 102 additions and 24 deletions
+16 -6
View File
@@ -13,6 +13,7 @@ import {
} from 'date-fns';
import { DateFormat } from '@/localization/constants/DateFormat';
import { TimeFormat } from '@/localization/constants/TimeFormat';
import { CustomError, isDefined } from 'twenty-shared/utils';
import { i18n } from '@lingui/core';
@@ -182,17 +183,26 @@ export const formatToHumanReadableDate = (date: Date | string) => {
return i18n.date(parsedJSDate, { dateStyle: 'medium' });
};
export const getDateTimeFormatStringFoDatePickerInputMask = (
dateFormat: DateFormat,
): string => {
const getTimePattern = (timeFormat: TimeFormat) => {
return timeFormat === TimeFormat.HOUR_12 ? 'hh:mm a' : 'HH:mm';
};
export const getDateTimeFormatStringFoDatePickerInputMask = ({
dateFormat,
timeFormat,
}: {
dateFormat: DateFormat;
timeFormat: TimeFormat;
}): string => {
const timePattern = getTimePattern(timeFormat);
switch (dateFormat) {
case DateFormat.DAY_FIRST:
return `dd/MM/yyyy HH:mm`;
return `dd/MM/yyyy ${timePattern}`;
case DateFormat.YEAR_FIRST:
return `yyyy-MM-dd HH:mm`;
return `yyyy-MM-dd ${timePattern}`;
case DateFormat.MONTH_FIRST:
default:
return `MM/dd/yyyy HH:mm`;
return `MM/dd/yyyy ${timePattern}`;
}
};