Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #2
mpdonadioComment #3
mpdonadio@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.
Comment #4
mpdonadioI am not sure if there really is a bug here. From the docs on the class
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).
Comment #5
renukakulkarni CreditAttribution: renukakulkarni as a volunteer and at Clarion Technologies commentedAs 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.
or
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.
Comment #6
mpdonadioWhen 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:
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.
Comment #7
zalak.addweb CreditAttribution: zalak.addweb commentedComment #8
jhedstromClosing this out for now.