It would be nice to have an option to choose the output format of the displayed date field. As there is no way for this currently (this part is hard-coded), I added a small change to the module. It is however available only programatically by now.
If applied, one would be able to add an option '#custom_format' => 'd-m-Y H:i' for example(or any other supported format) and the date would be displayed as desired.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tzandev created an issue. See original summary.

tzandev’s picture

StatusFileSize
new2.48 KB

Here is the patch.

chrbak’s picture

One way to change the above date format programatically is inside the HOOK_form_alter e.g.

  $form['my_field_name']['widget'][0]['value']['#custom_format'] = 'd/m/Y H:i';

Thank you for the patch.

blackiceua’s picture

StatusFileSize
new2.22 KB

Added possibility to use custom format for the Date only element.
Fixed ES6 error issue.

  • valic committed b6ea50a on 8.x-1.x authored by BlacKICEUA
    Issue #3033868 by tzandev, BlacKICEUA: Add option for custom date time...
valic’s picture

Status: Needs review » Active

I love the idea. Tnx on the initial patch. Committed in the dev version

The issue is on needs active still though I think would add this option to be configurable trough widget maybe.

edurenye’s picture

Shouldn't it use '#format' instead of '#custom_format' as with date type?

ytsurk’s picture

Thanks for all your work.

Right now this is not supporting type datenone, and not applied to the initial values.

phjou’s picture

Status: Active » Needs review
StatusFileSize
new2.79 KB

I just made a patch for adding support in the field widget. It was pretty easy since the support for the form element has already been committed. I just changed one thing from the previous commit:

'data-custom-format' => !empty($element['#custom_format']) ?? NULL,

to

'data-custom-format' => !empty($element['#custom_format']) ? $element['#custom_format'] : NULL,

It was necessary since the default config is an empty string, the test needs to be empty and not just isset.

Let me know if it working, seems good on my local.

phjou’s picture

Actually I just noticed that using the new custom_format broke completely the save action.

Doing something like this using the current API without patch, (as in comment #3) will break completely your form when submitting
$form['my_field']['widget'][0]['value']['#custom_format'] = 'd/m/Y H:i';

It crashes that error:
Exception: DateTime object not set. in Drupal\Component\Datetime\DateTimePlus->__call() (line 355 of /app/web/core/lib/Drupal/Component/Datetime/DateTimePlus.php).

phjou’s picture

Ok new patch containing the fix for the submission + @ytsurk comment about the default value.

ytsurk’s picture

Status: Needs review » Reviewed & tested by the community

Thx phjou!
Looks good. The format is now always initially set and I can confirm that saving work.
Don't remember what I meant with type datenone ..

jtriguero’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new4.11 KB
new1.77 KB

It doesn't work when you return from a failed validation to the form.
The problem is that the code returns in the valueCallback the date in format "Y-m-d \ TH: i: s".
Instead of trying to return the transformed value in the valueCallback, this should be done in the massageFormValues.
Attached patch and interdiff.

jtriguero’s picture

I forgot a "use".

spadxiii’s picture

StatusFileSize
new5.41 KB
new5.65 KB

Rerolled against latest dev and added the default-value formatting for the timestamp widget.

Otherwise it seems to work just fine.
A feature I was thinking about was to be able to select a date-format as defined in the regional settings in drupal. (With the option of a blank text-field as 'Other').

linkl’s picture

Fix the issue for date time range

spadxiii’s picture

Added a small typo-fix: $this-t() -> $this->t()

jcnventura’s picture

@SpadXIII, can you please update the issue summary with what your patch fixes, please? The current issue summary should be kept intact at the bottom, as I believe it reflects the situation before the commit in #5.

Ideally, also post screenshots of the before and after of the change as I believe that now the custom form is part of the widget settings.

Once I understand what the post-#5 patches are trying to add, I'll test and merge.

spadxiii’s picture

@jcnventura The commit in #5 only adds the option for the custom format so the javascript can handle a custom format.
The other patches add the option to configure it in the field widgets (through the field_ui interface)

jcnventura’s picture

OK. Good, I feel this would be a nice one to merge, but without it being RTBC, it may take some time.

spadxiii’s picture

StatusFileSize
new6.92 KB

Found a little bug with validating the value of a date range field. Fixed that in a new patch.
Also updated the private helper method for grabbing the date/time values.

spadxiii’s picture

StatusFileSize
new6.99 KB

Found another bug where it would still validate when leaving the end-date empty.

ps. an interdiff doesn't seem to generate : interdiff: Whitespace damage detected in input

spadxiii’s picture

StatusFileSize
new8.64 KB

Found another small issue where it would strip the time from the entered values. And the end-value wasn't handled correctly.

Here's a new patch.

idebr’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -date format

The new option to configure a custom format per field widget works as expected, thanks!

jcnventura’s picture

@idebr, Thanks for the review. Why did you hide the patch from #23 and #16. Are you saying the one you tested is the last one not hidden (#14) ?

Please clarify and hide all files other than the one you tested, please..

idebr’s picture

I was unaware I changed the displayed patches. I tested the latest patch in #23.

EricVL made their first commit to this issue’s fork.

idebr’s picture

Version: 8.x-1.x-dev » 2.x-dev
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new4 KB
new10.02 KB

Attached patch implements the following changes:

  1. Applied default value formatting to single_date_time_timestamp_widget and single_date_time_widget
  2. Applied massageFormValues() to all widgets that extend SingleDateTimeBase
anneke_vde’s picture

Status: Needs review » Reviewed & tested by the community

I tested patch #28, looks good the default_value formatting is now correct.

chewi3’s picture

StatusFileSize
new9.98 KB

Rerolled the latest patch for 2.0.0-beta2.

watergate’s picture

I can confirm that this works correctly. Thanks for the patch.

shaxa’s picture

StatusFileSize
new9.98 KB

Unfortunately, patch number 30 doesn't work for me. There is one small mistake which causes on each save to delete the date from the fields. I have changed $element['value']['#custom_format'] ?? $format to $element['value']['#custom_format'] ?: $format as the #custom_format is actually empty string by default.

jcnventura’s picture

Status: Reviewed & tested by the community » Needs review