Search code examples
javascriptlaraveldatedatatabledatatables

How to get dd-mm-yyyy format date in datatable laravel


I get the serverside data from controller, and get data using ajax. This is my datatable script.

// Data table
        var table = $('.data-table').DataTable({
            processing: true,
            serverSide: true,
            responsive: true,
            ajax: "quotation",
            dom: '<"top"fB>rt<"bottom"lip><"clear">',
            columns: [{
                    data: 'quotation_no',
                    name: 'quotation_no'
                },
                {
                    data: 'remarks',
                    name: 'remarks'
                },
                {
                    data: 'quotation_date',
                    name: 'quotation_date'
                },
                {
                    data: 'quotation_category',
                    name: 'quotation_category'
                },
                {
                    data: 'quotation_status',
                    name: 'quotation_status'
                },
                {
                    data: 'action',
                    name: 'action',
                    orderable: false,
                    searchable: false
                },
            ],
            "lengthMenu": [
                [10, 25, 50, -1],
                [10, 25, 50, "All"]
            ],
        });

default format for quotation_date is yyyy-mm-dd . How to make dd-mm-yyyy?


Solution

  • You can do something like this in the Model.

    /**
        * The attributes that should be cast.
        *
        * @var array
        */
        protected $casts = [
           'quotation_date' => 'datetime:d-m-Y',
        ];
    

    Or, alternatively you can also use Carbon Date in the controller:

    $quotationDate = Carbon::parse($item['quotation_date'])->format('d-m-Y');