Below configuration of "#date_time_format" for form element 'datetime' is not working :
$form['custom-module-datetime'] = array(
'#type' => 'datetime',
'#date_date_format' => 'm/d/Y',
'#date_time_format' => 'H:i A',
'#default_value' => DrupalDateTime::createFromTimestamp(time()),
);

Even if custom "date_time_format" is provided, Datetime module is setting the format to default one i.e. DateFormat::load('html_time')->getPattern(); (Datetime.php file of Datetime module in core).

Comments

renukakulkarni created an issue. See original summary.

mpdonadio’s picture

Priority: Major » Normal
mpdonadio’s picture

@renukakulkarni, what is your expectation here? I think this is a case of the documentation not matching the actual behavior, but I will dig into it tonight.

mpdonadio’s picture

Status: Active » Postponed (maintainer needs more info)

I am not sure if there really is a bug here. From the docs on the class

   *   - #date_date_element: The date element. Options are:
   *     - datetime: Use the HTML5 datetime element type.
   *     - datetime-local: Use the HTML5 datetime-local element type.
   *     - date: Use the HTML5 date element type.
   *     - text: No HTML5 element, use a normal text field.
   *     - none: Do not display a date element.
   *   - #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
   *     DateFormat::load('html_date')->getPattern().
   *   - #date_time_element: The time element. Options are:
   *     - time: Use a HTML5 time element type.
   *     - text: No HTML5 element, use a normal text field.
   *     - none: Do not display a time element.
   *   - #date_time_format: A date format string that describes the format that
   *     should be displayed to the end user for the time. 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
   *     DateFormat::load('html_time')->getPattern().

So, the only way to use custom formats is to use text inputs. Does that help? To be honest, though, I am not sure if this is fully tested with inputs other than 'date' and 'time' (I am not sure if we have test coverage at all for this element).

renukakulkarni’s picture

Status: Postponed (maintainer needs more info) » Active

As per the below documentation provided for "#date_itme_format" , this setting should work with HTML5 field -
* #date_time_format: A date format string that describes the format that
* should be displayed to the end user for the time. 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
* DateFormat::load('html_time')->getPattern()

and as per this link , "datetime" is a HTML5 field.

So as per above documentation either of below two codes should work while creating a datetime field in a custom form in drupal 8.

    $form['custom-datetime'] = array(
      '#type' => 'datetime',
      '#date_date_format' => 'm/d/Y',
      '#date_time_format' => 'H:i A',
    );

or

    $form['custom-datetime'] = array(
      '#type' => 'datetime',
      '#date_date_format' => 'm/d/Y',
      '#date_time_format' => DateFormat::load('custom_created_datetime_format')->getPattern(),
    );

None of the above code is working and I am only getting default time set which is in the format "H:i:s".
"#date_time_format" setting is not working.

mpdonadio’s picture

Category: Bug report » Support request

When you create a '#type' => 'datetime' form element, the date and time entry subelements default to 'date' and 'time' which use the HTML5 input elements, so the patterns will be 'Y-m-d' and 'H:i:s', respectively, as these are the HTML5 strings:

When using HTML5 elements the format MUST use the appropriate HTML5 format for that element, no other format will work.

If you want different formats then you need to use '#date_date_element' => 'text' and/or '#date_time_element' => text, and then specify your format strings.

Just keep in mind a quirk that is still being ironed out, #2723159: Datetime form element cannot validate when using a format without seconds.

zalak.addweb’s picture

Issue tags: +datetime form element
jhedstrom’s picture

Status: Active » Closed (works as designed)

Closing this out for now.