7

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(),
Stan Barrows
  • 873
  • 1
  • 12
  • 27

4 Answers4

14

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') : '')
Grant
  • 5,709
  • 2
  • 38
  • 50
12

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/

r00t
  • 468
  • 3
  • 10
4

use this, hope it will works

Date::make('start')->format('F j, Y'),
Shailendra Gupta
  • 1,054
  • 6
  • 15
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');
    }