1

How to change native easy admin 3 in symfony date/time picker time format? It now renders month, day, year. How do you set to 'Y-m-d H:i'?

Efkiss
  • 95
  • 3
  • 10

1 Answers1

5

If you look in the source code for the DateTimeField you will find a setFormat method that accepts a valid ICU Datetime Pattern (see http://userguide.icu-project.org/formatparse/datetime).

The format of a native date picker is determined by the browser's native code and locale settings, you cannot change it with out some complicated scripting (see here). This may possibly update the format of a choice or text type (not tested).

Try something like this:

use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;


public function configureFields(string $pageName): iterable
{
    return [

        // native date picker format cannot be changed
        // Uses native HTML5 widgets when rendering this field in forms.
        DateTimeField::new('beginsAt')->setFormat('Y-MM-dd HH:mm')->renderAsNativeWidget(),

        // Uses <select> lists when rendering this field in forms.
        DateTimeField::new('beginsAt')->setFormat('Y-MM-dd HH:mm')->renderAsChoice(),

        // Uses <input type="text"> elements when rendering this field in forms.
        DateTimeField::new('beginsAt')->setFormat('Y-MM-dd HH:mm')->renderAsText(),
    ];
}
Arleigh Hix
  • 9,990
  • 1
  • 14
  • 31
  • 1
    It does change how date is rendered in crud index page, but it does not change native date picker widget format. – Efkiss Mar 01 '21 at 06:43
  • @EvaldasAi you cannot change the format of a native date picker, the browser has total control over that and its based on the user's locale. I'll edit my answer, maybe it will work for formatting choice or text. – Arleigh Hix Mar 01 '21 at 07:57