-1

I have the following:

$('#search').on('keyup',function(){
var value = $(this).val();
$.ajax({
    type : 'POST',
    url : '{{ route("search") }}',
    dataType: 'json',
    data: {
        '_token' : '{{ csrf_token() }}',
        value : value,
        company_id : '{{ request()->company_id }}'
    },
    
    success: function(res){
        var tableRow = '';

        $('#table-content').html('');

        $.each(res, function(index, value){
            tableRow = `<tr>
                            <td hidden> ${value.id} </td>
                            <td> ${value.employee_number} </td>
                            <td> ${value.name} </td>
                            <td> ${value.code } -  ${value.description } </td>
                            <td> ${value.quantity } </td>
                            <td> ${value.novelty.unit } </td>
                            <td> ${value.date} </td>
                            <td> ${value.informed}</td>
                        </tr>`
            
            $('#table-content').append(tableRow);
        });
    }
});

Its working OK but the date its printing like this: Image

And I want the date to print in d-m-Y format. I've been playing a lot with different options but none of them worked for me.

EDIT: I'dont know if it changes something but that template literal its inside the success function of an ajax request.

FranVillada
  • 21
  • 1
  • 9
  • See https://stackoverflow.com/questions/13459866/javascript-change-date-into-format-of-dd-mm-yyyy for a helper function you could pass your "value.date" – Tonton-Blax Aug 05 '21 at 14:45
  • Thanks!! @Tonton-Blax the first answer on that question worked like a charm. I've only had to add +1 to the date because for some reason it was always giving me a the day before. I image its something to do with timezones – FranVillada Aug 05 '21 at 15:00

1 Answers1

0

Try using this, Option 1:

value.date.toLocaleDateString(); //  d/m/y

Option 2:

value.date.getDate() + "-" + (value.date.getMonth() + 1) + "-" + value.date.getFullYear()

And also please visit here for more details regarding the javascript date.