Laravel Nova: Convert Datetime to readable output
Asked Answered
E

4

8

Is there an option in Laravel Nova to display an readable date-time output and/or limit the output?

For example to : 29. October 2018 / 11. November 2018, 12:10 am

Datetime

Code:

   DateTime::make('Start')
                ->rules('required')
                ->sortable(),
Extravagancy answered 30/10, 2018 at 8:47 Comment(1)
show your blade code instead of an imageArmanda
H
20

This is achieved in Nova 4.0+ with the displayUsing method:

DateTime::make('Updated', 'updated_at')
->displayUsing(fn ($value) => $value ? $value->format('D d/m/Y, g:ia') : '')
Helios answered 21/9, 2022 at 7:6 Comment(0)
C
11

As per documentation https://nova.laravel.com/docs/1.0/resources/fields.html#datetime-field

use Laravel\Nova\Fields\DateTime;

DateTime::make('Start')->format('DD MMMM YYYY'),

Must use Moment.js formatting rules https://momentjs.com/docs/#/displaying/format/

Crossstaff answered 30/10, 2018 at 10:11 Comment(1)
Does not work in laravel nova 4.*Iou
A
3

use this, hope it will works

Date::make('start')->format('F j, Y'),
Armanda answered 30/10, 2018 at 9:9 Comment(2)
This is not working on Laravel Nova 4.*Roe
See my answer above for Nova 4.*Helios
S
0

We also faced such a problem. This solution DateTime::make('Start')->format('DD MMMM YYYY'), helps only for index page, but didn't help for Edit page. I don't know when this bug will be fixed in new Nova releases but we temporary used small hardcoding.

    1. nova/src/Fields/Date.php
 instead: return $value->format('Y-m-d');
 use this one: return $value->format('m/d/Y');
    1. nova/resources/js/components/Form/DateField.vue
 In this vue component also should be changed a date format: dateFormat="m/d/Y".
    1. nova/resources/js/components/Form/DateField.vue
 For placeholder method use this one:
     return this.field.placeholder || moment().format('MM/DD/YYYY')
Instead this:
     return this.field.placeholder || moment().format('YYYY-MM-DD')
    1. Also You should use Mutator in Your App\Model class if you store data in the database in another format. Something like this:

    public function setLastUsedAttribute($value){
          $date = Carbon::createFromFormat('m/d/Y', $value);
          $this->attributes['last_used'] = $date->format('Y-m-d');
    }

Strophanthin answered 28/3, 2019 at 16:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.