Chakra UI DatePicker v2 retains browser time‑zone in returned Date despite locale setting
0 reputation · 07 Nov 2021, 14:16 UTC
0 reputation · 07 Nov 2021, 14:16 UTC
The goal is to configure Chakra UI’s DatePicker (v2) so that the value passed to onChange represents a UTC timestamp while the calendar continues to display the date according to the chosen locale (e.g., 'fr-FR'). This would eliminate the need for manual offset conversion in application code and ensure consistent date handling across users in different time zones.
Currently the component returns a Date object set to midnight in the browser’s local time‑zone, and no prop or configuration option exists to alter this behavior. The documentation advises using external libraries such as date‑fns‑tz for zone conversion, but it is unclear whether a built‑in timezone‑aware mode is planned or if an undocumented setting could enable UTC output.
35275 reputation · 07 Nov 2021, 20:29 UTC
Chakra UI’s DatePicker v2 does not expose any prop or configuration flag that forces the onChange callback to return a UTC Date. The returned value is always a Date set to midnight in the browser’s local time‑zone.
The locale prop (e.g., locale='fr-FR') only influences the calendar’s display formatting—month names, day names, first‑of‑week, etc. It does not affect how the internal Date object is created, so the timezone of the returned value remains tied to the user’s environment.
locale prop for proper UI localization.onChange handler, convert the received local‑midnight Date to a UTC instant using a timezone‑aware library such as date‑fns‑tz:import { zonedTimeToUtc } from 'date-fns-tz';
function handleChange(date) {
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const utcDate = zonedTimeToUtc(date, timeZone);
// utcDate now represents the same instant but expressed in UTC
console.log('UTC date:', utcDate.toISOString());
}
If you prefer not to add a dependency, you can obtain the ISO string and treat it as UTC:
function handleChange(date) {
const iso = date.toISOString(); // already in UTC
// Store or send iso directly; avoid new Date(iso) if you need a UTC‑based Date
}
<DatePicker locale='fr-FR' onChange={d => console.log('raw:', d, 'ISO:', d.toISOString())} />, select a date, and confirm the logged Date’s hours/minutes/seconds are zero and correspond to the browser’s local midnight.utcDate.toISOString() matches the original date’s ISO string shifted to UTC (i.e., the same instant).As of the latest public documentation and roadmap, Chakra UI has not announced a timezone‑aware mode or a UTC‑output flag for the DatePicker v2 in any upcoming release.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.