Problem/Motivation

A number of users have become confused around the datetime element's format attributes #date_date_format and #date_time_format.

If you use the HTML5 form elements (which are only supported in some browsers, others make them behave like text fields), then the date/time formats *have* to be the HTML5 flavors ('Y-m-d' and 'H:i:s'). And per Datetime::getInfo(), they default to the HTML5 inputs when not specified.

Part of the problem comes from the docs being in a weird place for this form element.

Proposed resolution

Possible solutions include:

  1. Improve the docblocks and/or move some of the information around, perhaps much on Datetime::processDatetime() should be moved to the top level comment on the class (and some of that may not be accurate in Drupal 8 anymore).
  2. Trigger a notice when custom formats are specified with the HTML5 elements.
  3. If a custom format is specified, then force the element to 'text' (maintainer is not keen on this, and it has probably has BC consequences).

Remaining tasks

Discuss, agree, code.

User interface changes

None

API changes

TBD

Data model changes

None.

Original Summary

We are getting error - Error : "The date is invalid. Please enter a date in the format 2017-05-03 12:57:23". even if we provided expected format in '#date_date_format' & '#date_time_format' in our custom form field - datetime as below code.

$form['datetime'] = [
  '#type' => 'datetime',
  '#size' => 20,
  '#date_date_format' => 'd/m/Y',
  '#date_time_format' => 'H:m',
];

Value Provided on Form Submission : "03/05/2017 12:57:23"

Expected format : "d/m/Y" but Drupal 8 - DateTime supports only "Y-m-d".

I guess, Drupal 8 follow the HTML5 () default date format. Please skip this validation if user provided their own date formats. Please help us to resolve this.

--
Thanks
Vignesh.R

CommentFileSizeAuthor
#17 date-format-difference.png25.3 KBvickey

Comments

vickey created an issue. See original summary.

vickey’s picture

Version: 8.3.0 » 8.3.1
jonathanshaw’s picture

Category: Bug report » Feature request
Priority: Major » Normal

The docs say:

#date_date_format: A date format string that describes the format that should be displayed to the end user for the date. When using HTML5 elements the format MUST use the appropriate HTML5 format for that element, no other format will work. See the format_date() function for a list of the possible formats and HTML5 standards for the HTML5 requirements. Defaults to the right HTML5 format for the chosen element if a HTML5 element is used, otherwise defaults to

You should be able to work round this without too much difficulty in a custom widget extending one of the core widgets.

jonathanshaw’s picture

Title: Date Invalid Error » #date_date_format limited to HTML5 formats
saru1683’s picture

I had same issue, here is the link which one useful to change​ the date format Datetime widgets

mpdonadio’s picture

Title: #date_date_format limited to HTML5 formats » Provide better information around Datetime element attribute combinations.
Version: 8.3.1 » 8.3.x-dev
Category: Feature request » Support request
Issue summary: View changes
Issue tags: -Drupal 8.x, -datetime +DX (Developer Experience)

Expanding on #3, this is really a problem of the docs being in a weird place for this form element. Per

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Datetime%...

if you use the HTML5 form elements (which are only supported in some browsers, others make them behave like text fields), then the date/time formats *have* to be the HTML5 flavors ('Y-m-d' and 'H:i:s'). And per Datetime::getInfo(), they default to the HTML5 inputs when not specified.

If you want to use your own formats, you have to explicitly make the form elements 'text'. The following should work:

$form['datetime'] = [
  '#type' => 'datetime',
  '#size' => 20,
  '#date_time_element => 'text',
  '#date_date_format' => 'd/m/Y',
  '#date_time_element' => text',
  '#date_time_format' => 'H:m',
];

This topic has come up enough times that I think we need a better solution here. I think there are a few (potentially not mutually exclusive) options here:

- Improve the docblocks and/or move some of the information around, perhaps much on Datetime::processDatetime() should be moved to the top level comment on the class (and some of that may not be accurate in Drupal 8 anymore).
- Trigger a notice when custom formats are specified with the HTML5 elements.
- If a custom format is specified, then force the element to 'text' (not keen on this, and it has probably has BC consequences).

Open to other suggestions. I like the first two.

mpdonadio’s picture

Category: Support request » Task
vickey’s picture

Hi Folks,

Thanks for taking care of my issue.

- Thanks for your response. It did some work around below.

- The reference link is useful but we don't want to introduce more complexity to fix this.

- Thank you for the fix but if we applied '#date_time_element => 'text' attribute, date_popup is not displayed but we need to date_popup feature also.

I did work around in our custom module that, we have excluded the date default validation(HTML5) by using '#element_validate' => array() attribute like below, now default HTML5 validation error(actual) not displayed and i have written our own validation in validateForm().

$form['datetime'] = [
  '#type' => 'datetime',
  '#size' => 20,
  '#date_date_format' => 'd/m/Y',
  '#date_time_format' => 'H:m',
  '#element_validate' => array(),
];

Is this standard work around for this issue? Please let me know your thoughts on this.

jonathanshaw’s picture

Title: Provide better information around Datetime element attribute combinations. » Datetime element formats are confusing
Issue summary: View changes
if we applied '#date_time_element => 'text' attribute, date_popup is not displayed but we need to date_popup feature also.

I don't really understand what you need here. If you're using a popup, why care about format?

Anyway, the issue queues for Drupal core aren't typically the best place for support questions around the details of your specific workaround. To get more support, I suggest you post a detailed question on Drupal answers.

#6 is a good direction for this issue, I'll update the IS to fit that.

jonathanshaw’s picture

Issue summary: View changes
vickey’s picture

Hi jonathanshaw,

It is Drupal Default Date Popup provided by Core. We are not using any external popup plugin here

Screenshot here

Drupal 8 force use HTML5 format('Y-m-d') even if we choose our custom format(d/m/Y) using date popup and submitting the form, Drupal 8 - it throws error that, "invalid date". we need both date popup and custom format(d/m/Y). If we use "date_time_element" => "text", date popup will be removed as per your suggestion, we need to implement the date popup using own custom js code. We don't want to introduce more complexity on this because Drupal 8 core has that feature.

Hope, this will help to understand our current situation.

--
Thanks
Vignesh.R

mpdonadio’s picture

#11, can you explain a little more why you need the popups and your own custom format? The custom format is used to parse the input, but if it is coming from the popup, it isn't needed. If you need the date in another format in code, you can just convert it in your submit handler.

vickey’s picture

Date popup is make life easy to end user who don't have knowledge about date format and they will use this option to select the prefered date, custom format is essential(preference) as they are using in their circumstance after they selected date from the popup. As per your last comments, we could convert it in submit handler but we are not able go there due to default Date validation("Invalid date") from core. reference screenshot here - #11.

Expectation :
We can use custom format along with date_popup, it is accepting now except that "Invalid date" error provided by Core - datetime module. We expects, if developer provides custom format, core should validate the given custom format, not to force HTML5 date format, it will be irrelevant.

It is good to participate people to this discussion. thanks for your response - mpdonadio.

mpdonadio’s picture

#13, in your submit handler `$form_state->getValue('datetime') will be a DrupalDateTime object built from the date/time in your form. You can use that directly, or if you need the 'd/m/Y', you can just `$form_state->getValue('datetime')->format('d/m/Y')`

vickey’s picture

#14, yes we could. I am forcing custom format('#date_date_format' => 'd/m/Y') for end user while choosing the date popup - this execution doesn't go to submit handler because it is already stopped in validation itself due to below error. it is not allow to submit the form with the custom format.

Error Message : The date is invalid. Please enter a date in the format 2017-05-03 12:57:23

We are looping continuously without understanding actual issue place. Let me put it this way, If we get the answer for the below questions, we will have to expected solution in hand.

Questions are,
1. Why Drupal 8 force - HTML5 date format(Y-m-d) even we used custom format('#date_date_format' => 'd/m/Y')? this is not scenario in Drupal 7.
2. How to resolve above error without using "#date_date_element" => 'text' as we need to popup feature also.
3. How to code date & time popup field in Drupal 8 using custom format('#date_date_format' => 'd/m/Y')? Please provide your patch code before make sure above error will not occur.

mpdonadio’s picture

#15, per https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

One thing to note is that the displayed date format differs from the actual value — the displayed date format will be chosen based on the set locale of the user's operating system, whereas the date value is always formatted yyyy-mm-dd

.

Drupal 8 uses HTML5 input elements, so the date popup is either from the browser itself with those that support it (eg, Chrome, iOS) or the polyfill from those that don't (eg, Firefox but not 100% sure if that is still the case). Drupal 7 uses text inputs w/ the jQuery UI library for the popup.

If you want the popup, you aren't going to be able to use `'#date_date_format' => 'd/m/Y'`. You will need to retain the 'Y-m-d' format in the $form element, and then convert the value to 'd/m/Y' in the submit handler. My working code for this is on my other machine I; I will post a sample later.

vickey’s picture

StatusFileSize
new25.3 KB

Hi All,

HTML DATE FORMAT & POPUP VIEW is differ based on browser which we are using to viewing the form page.

I have tested it both in IE-11 & Chrome 57. Please find the attached screenshot here.

IE 11 - Supports Default HTML Date format - YYYY-MM-DD
Chrome 57 - Supports Different Date format - MM-DD-YYYY

Even if we are not using the custom format ('#date_date_format' => 'd/m/Y'), date field is taking different format and different view of date popup based on browser which we are using to viewing.

In Drupal 8, We want to display the date field with uniform format and date popup for all the browsers. Please share your thoughts on this and guide us.

jonathanshaw’s picture

I suspect these are display formats that will depend on the browser's locale. This fiddle might help you explore this behavior: http://jsfiddle.net/to311fj5/

You may be able to avoid this if you don't use the core datetime (HTML5) element but instead a jquery datepicker. But please discuss that on Drupal Answers, not here in the Core issue queues.

vickey’s picture

Thanks for your response. Let me try to use jquery datepicker, will share patch-code once its works fine. If any issues, i will post it on Drupal Answers.

skaught’s picture

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

eojthebrave’s picture

I'm trying to get this working, so that I can then update the documentation. Here's what I'm encountering thus far.

Trying an element like the following, which uses a custom date, and time, format, doesn't work.

    $form['datetime_custom'] = [
      '#title' => 'Custom format datetime element',
      '#type' => 'datetime',
      '#size' => 20,
      '#date_date_element' => 'text',
      '#date_date_format' => 'd/m/Y',
      '#date_time_element' => 'text',
      '#date_time_format' => 'H:i',
    ];

When the form is submitted, during validation, \Drupal\Core\Datetime\Element\Datetime::valueCallback is called to get the value of the field, which in turn uses \Drupal\Component\Datetime\DateTimePlus::createFromFormat. createFromFormat in this case throws an exception here:

      if ($settings['validate_format'] && $test_time != $time) {
        throw new \UnexpectedValueException('The created date does not match the input value.');
      }

This is because $test_time, and $time are not equal.

When you enter the value 4/3/1982 10:10 into the input field, it creates a valid DateTime object. Then, later in the method it runs this $test_time = $date->format($format); to get the test time. $format = 'm/d/Y H:i' from the element above. And you end up with $time = '4/3/1982 10:10' and $test_time = '04/03/1982 10:10' because m, and d, and H, and i all allow for optional leading zeros. So, the check fails, and expection is thrown, and the element doesn't validate. But ... it really should.

I think in addition to updating the documentation this problem should probably also be fixed.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

liquidcms’s picture

Is any of this covered by documentation? Specifically, I had been trying to use a "date" form element (rather than "datetime" element) but seeing now that "date" element does not work correctly: #2944552: Documentation for #default_value in Date form element is incorrect.

Switching my element to "datetime" does work correctly, but, it displays time, which i do not want. Dying to know where documentation is so that I can disable time portion of field.

mpdonadio’s picture

#24, oddly it's all on Datetime::processDatetime. See my comment on #6 about potentially moving this to a better place.

brianwagner’s picture

Hiding the 'time' element is possible.

I struggled with this like liquidcms in #24, as I was using the 'date' form element.

Docs here show you need to:

- use the 'datetime' field
- specify '#date_date_element' as 'date'
- specify '#date_time_element' as 'none'

Also, this allows you to set default value, which requires a DrupalDateTime object. The 'date' field will not accept a default value.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tostinni’s picture

Thank you @brianwagner for your comment it is very handy :-)

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

avpaderno’s picture

Version: 9.2.x-dev » 9.4.x-dev

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jaime@gingerrobot.com’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mstrelan’s picture

johnv’s picture

Yes, this still is a DX problem.
In the contrib field Office Hours, maintainer each time struggles with this, whenever there is an issue with the HTML5 setting. For a 'modern' UX element, persons expect the 'ampm' time format to be supported. If this si not the case, this must be perfectly clear. Also, as stated above, the developers machine may not support all OS-version in the field.

johnv’s picture

To add a note: especially ampm vs. 24-hours notation and with: #2811507: Date hour format is hard coded and can't be override

skaught’s picture

#37
yes, this wider issue is still relevant. Core's date wow's are 'as tough to deal with as ever'
1 .as a dev working on client project. core date field/widgets lack in many many ways!!!!
2. as a user filling out date forms in drupal sites.. simple test 'all the date widgets' drupal provides, not just daterange (:

solideogloria’s picture

I agree that the datetime UI element needs work and that the am/pm format should be supported by default.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

quietone’s picture