1

I have a date field on a front end form, currently with this HTML:

<input class=" date-picker startDate" type="text" id="startDate" name="fields[startDate]" value="">

I use a JQuery date-picker and have set its format to dd/mm/yyyy

The date displays in the field this format correct, but upon saving no date is saved. I guess its due to the format not being Crafts yyyy-mm-dd because if I set the date-picker format to this, it does save it OK. BUT I obviously dont want to display my dates like this.

How can I display dates in correct human date format in the fields, and save them to the DB correct? Do I need to process all dates in PHP in a plugin to the correct format first?

The field in Craft admin for the entry displays d/m/y OK and that saves.

Thanks

1

For me, saving dates as a timestamp seems to do the trick to correctly save to the date field in Craft. You can still use the datepicker and have the human readable date, just use the altField and altFormat options (https://jqueryui.com/datepicker/#alt-field) to fill a hidden field with the timestamp that gets submitted.

1
  • Thank you. I was using fdatepicker which I dont think has altField. I switched to Jquery datepicker but altField using a jQuery selector does not work for me. It works when referencing the actual class, but not with a selector. I need to use this selector as I have multiple date fields: altField: $(this).next() -> doesn't work! – Laurence Cope Jul 10 '20 at 16:28
1

Maybe this reference helps you; https://docs.craftcms.com/v3/date-time-fields.html

Your date field will be like this.

{% set tz = craft.app.getTimezone() %}
{% set df = craft.app.locale.getDateFormat('short', 'php') %}

<input type="text" id="startDate" name="fields[startDate][date]" {%- if user is defined %} value="{{ user.startDate|date(df, tz) }}"{% endif %}>
    {{ hiddenInput('fields[startDate][timezone]', tz) }}
</div>

Regarding date format, In front-end form, date format should be YYYY-MM-DD or current locale's short date format.

If the locale is "en", the date format is "m/d/y". Perhaps it is "d/m/y" if the locale is "en-gb" (not "en"), but I am not sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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