2

In my plugin I have a datetime field defined in the settings template:

{{ forms.dateTimeField({
    label: "Ends at"|t('optimum'),
    instructions: experiment.isNew ?  'Defaults to 30 days from now' : '',
    id: 'endAt',
    name: 'endAt',
    value: experiment.endAt ?? defaultEndAt,
    errors: experiment.getErrors('endAt')
}) }}

When I use the picker I get a date in mm/dd/yyyy format. This is then sent to the backend and parsed back to a Carbon instance.

This is what I'm seeing on my end:

enter image description here

However, it seems that the date format is not consistent across installations. As you can see in this issue, the user reports that the datepicker populates the date in dd/mm/yyyy format, which breaks the functionality as the backend is expecting mm/dd/yyyy format.

How can I enforce a uniform format?

1 Answer 1

2

All dates in the control panel are rendered localized as per the logged-in user's "Formatting Locale" user preference, or their "Language" setting if they haven't set a preferred locale.

For users who haven't set a preferred locale or language, Craft will fall back to the locale defined in the defaultCpLocale setting.

1
  • 1
    Thanks! I see now that I should use Craft's own DateTimeHelper::toDateTime($dateString) instead of Carbon, to get a reliable parsing of the input. This does all the heavy-lifting of figuring out the input format and building a DateTime object. See GraphqlController:464 for an example of how Craft applies the same principle.
    – Matanya
    Sep 9, 2023 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.