I have javascript function that pull data from the database and give the date output. This part of the output scripts: `data: { columnsDef: ['letter_number', 'letter_date', 'job_desc'], }, },

    columns: [

        { data: 'letter_number', name: 'letter_number' },
        { data: 'letter_date', name: 'letter_date' },
        { data: 'job_desc', name: 'job_desc' },
    ],`

The letter_date output was 2019-04-29 and I intend to change the format to 29-04-2019, I've already tried some javascript dateformat function on the top of the scripts but the page just blank no output showed on the page. I'm using laravel and using Carbon library for other date format in laravel code tag and there are no problem.

I'm confused how to change the format inside the Javascript output code like shown above { data: 'letter_date', name: 'letter_date'}.

Any solution I'll appreciate it very much. Thank you.

4 Answers

0
Koban Davis On

If the format is consistent, you could split on each dash and rejoin them in the desired format:

const date = '2019-04-29'

const dateArr = date.split('-')
const newDate = `${dateArr[2]}-${dateArr[1]}-${dateArr[0]}`
console.log(newDate)

0
Murtaza Hussain On

Use moment to format your date as you want

This is the most robust display option. It takes a string of tokens and replaces them with their corresponding values.

moment(new Date()).format();                                // "2014-09-08T08:02:17-05:00" (ISO 8601, no fractional seconds)
moment(new Date()).format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"

in your context:

moment(new Date()).format('DD-MM-YYY');
0
Mosi On

I think you used DataTable plugin for showing data in grid. you can change values in createdRow.

...
columns: [
    { data: 'letter_number', name: 'letter_number' },
    { data: 'letter_date', name: 'letter_date' },
    { data: 'job_desc', name: 'job_desc' },
],
createdRow: function(nRow, aData, iDataIndex){
    const d= aData.letter_date.split('-');
    $('td:eq(1)',nRow).html(d[2]+'-'+d[1]+'-'+d[0]);
}
...
0
Kris On

I got this solution, first of all I'm using datatables plugin in my laravel. Using Yazra datatables, and use Carbon libraries to convert the date format. I found this solution on github posts. This the link: DateTime Format Yazra Datatables and Carbon Plugin in laravel

This working great, and actually there are connection between my Controller on laravel and Column on Javascript as used on Yazra datatables. Hope this help anyone.

Thank you so much for all the solution above...Thank you.