#Resolving DatePicker display conflict with ISO 8601 input value

24 messages · Page 1 of 1 (latest)

proven zephyr
#

Wondering if anyone knows how to get the datepicker to play nice with ISO 8601 formatted values? My data layer is passing around dates formatted like 2022-09-06 on par with ISO 8601, and while a normal date input can handle this just fine, the mantine datepicker has issue. Particularly, it seems to store the value properly, but not display it in the input.

example: https://codesandbox.io/s/mantine-quirks-forked-wfnmjn?file=/src/App.tsx

mantine quirks (forked) by pdevito3 using @emotion/react, @heroicons/react, @mantine/carousel, @mantine/core, @mantine/dates, @mantine/form, @mantine/hooks, @mantine/modals, @mantine/notifications

nimble wyvern
# proven zephyr Wondering if anyone knows how to get the datepicker to play nice with ISO 8601 f...

dateformat for mantine (forked) by mpyoo using @emotion/react, @heroicons/react, @mantine/carousel, @mantine/core, @mantine/dates, @mantine/form, @mantine/hooks, @mantine/modals, @mantine/notifications

Capture date or dates range from user

proven zephyr
#

@nimble wyvern yeah, i've tried that as well, but it seems like it's doing new Date() under the hood and transforming it to local time and giving me -1 days

chilly crater
nimble wyvern
#

i think this is a dayjs under the hood thing

proven zephyr
#

Not sure exactly what the cause it but the same thing is happening on my box that’s happening in codesanbox

nimble wyvern
#

yeah im fairly certain there's consistent offset

proven zephyr
#

Right but how can I tell it to change the behavior?

nimble wyvern
#

actually yeah, this looks like a timezone difference thing

proven zephyr
#

But if it’s just sending it back as a date I just want it to be midnight of that date regardless to time zone

nimble wyvern
#

i understand, im investigating

proven zephyr
#

Preciate ya

nimble wyvern
#

also note: you can override toLocaleDateString() behaviour. so if my set to midnight solution does not work, you can coerce the timezone
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString#try_it

The toLocaleDateString() method returns a string with a language-sensitive representation of the date portion of the specified date in the user agent's timezone. In implementations with Intl.DateTimeFormat API support, this method simply calls Intl.DateTimeFormat.

proven zephyr
#

Nice! In the car on my phone but I’ll be back at my computer in about 10 minutes to confirm, but it seems like that does it, thanks for the help 🍻

proven zephyr
#

hmm, actually it seems like it's resetting the time, but to the day prior

proven zephyr
proven zephyr
#

just realized the codesanbox doesn't even fix the display on blur, but the date does still seem to show as selected on the dropdown. very weird

nimble wyvern
#

otherwise, try to force the locale formatting

proven zephyr
proven zephyr
#

FYI, seems that dayjs figured this all out under the hood if you just use their toDate() method:

        value={
          value === null || value === undefined ? null : dayjs(value).toDate()
        }
        onChange={(date) => {
          if (date !== undefined && date !== null) {
            onChange(dayjs(date).toDate());
          } else onChange(null);
        }}

https://day.js.org/docs/en/display/as-javascript-date

nimble wyvern
#

great find, as a quick note you could simplify value to

{value ? dayjs(value).toDate() : null}

and onChange to

onChange(date ? dayjs(date).toDate() : null)