简体   繁体   中英

Format date as dd.mm.yyyy with Moment.js and Datatables

I would like to render the date which is in this format in datatables: 29.01.2021 (today's date for example)

I have tried below code, but it will output as "invalid date".

$(document).ready(function(){
        $.fn.dataTable.moment('DD.MM.YYYY');
...

{ data: 'db_date',
    render: function (data, type, row) {
      return moment(new Date(data).toString()).format('DD.MM.YYYY');
    }
}

What could be missing? - Thanks!

Remove the toString() method from the Date object as shown below.

 $(document).ready(function(){ $.fn.dataTable.moment('DD.MM.YYYY'); ... { data: 'db_date', render: function (data, type, row) { return moment(new Date(data)).format('DD.MM.YYYY'); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM