1

I've stored date to my DB in 2019-12-20 11:45 format. But I need it to show in my data table in a 20-12-2019 1:45 PM format. I use jQuery data table.

{
    "render": function (data, type, content, meta) {            
        return content.starting_time;
    }
},

How do I convert a date returned by this function?

Pingolin
  • 3,161
  • 6
  • 25
  • 40
techLad
  • 73
  • 7
  • 1
    Does this answer your question? [How to reverse date format yyyy-mm-dd using javascript/jquery?](https://stackoverflow.com/questions/40232218/how-to-reverse-date-format-yyyy-mm-dd-using-javascript-jquery) – prasanth Dec 19 '19 at 06:31
  • no. it doesnt say anything about the time. – techLad Dec 19 '19 at 06:36
  • you should split the time and date first .Then do with above answer https://jsfiddle.net/prasanth1036/v8csg6z4/2/ .And `11:45` not equal to `1:45 PM` .Or else try with [moment.js one format to another](https://stackoverflow.com/questions/38251763/moment-js-to-convert-date-string-into-date) – prasanth Dec 19 '19 at 06:41

4 Answers4

1

There are two ways in which you can do it.

  1. Directly add condition in the data table,to change the receiving date for
{
    "data": "createdTime",
    "render": function (data) {
        var date = new Date(data);
        var month = date.getMonth() + 1;
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var ampm = hours >= 12 ? 'pm' : 'am';
        return (month.toString().length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear() + ampm;
    }
}
  1. You can use carbon date in laravel and change the receiving date format.
'Carbon' => 'Carbon\Carbon'

return Carbon::createFromFormat('d-m-Y h:i A',content.starting_time)

1

With plain JS you can do like below:

var date = new Date("2019-12-20 13:45");
console.log(date.toLocaleDateString("en-GB").split('/').join('-') + ' ' + date.toLocaleTimeString('en-US', {hour: '2-digit', minute: '2-digit'}));

Hope it helps.

Piyush
  • 589
  • 4
  • 11
0

$TIME = date('d-m-Y h:i A', strtotime($YOUR_DATE_TIME)); FOR MOR INFO https://www.php.net/manual/en/function.date.php

albus_severus
  • 3,626
  • 1
  • 13
  • 25
0
<?php

use Carbon\Carbon;
$data =  Carbon::parse('2019-12-20 11:45')->format('d-m-Y h:i A');
dd($data);

?>

Hope this helps

NutCracker
  • 11,485
  • 4
  • 44
  • 68