Datatables日期时间格式排序问题

Datatables datetime format sorting problem

我想格式化日期列。 (DD.MM.YYYY HH:mm ) 我也收到订单错误。排序在此格式中不起作用。

1.) 你有链接到你的 HTML 的时刻脚本吗?

如果不确定,请在您的 html <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" referrerpolicy="no-referrer"></script>

<head> 标签中添加以下内容

如果是的话。您可以执行以下操作。

您实际上可以像这样在列初始化中重新格式化日期,而不是在 columnDefs 中呈现它:

moment.suppressDeprecationWarnings = true;

var dataSet = [
{
"Something" : "Something1",
"Date" : "01/12/2021"
}
]

$(document).ready(function() {
    $('#example').DataTable( {
        data: dataSet,
        columns: [
            { "data": "Something" },
            { "data": "Date",
            render: function(data, type, row) { //render function to format the date values from my AJAX 
                if (type === "sort" || type === "type") {
                    return data;
                }
                return moment(data).format("DD.MM.YYYY HH:mm");
                }
                }
        ]
    } );
} );
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <!--stylesheets-->
      <!-- <link rel="stylesheet" href="site.css"> -->
      <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css"/>
      <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs4-4.6.0/jq-3.3.1/dt-1.11.0/af-2.3.7/date-1.1.1/r-2.2.9/rg-1.1.3/datatables.css"/>
      <!--scripts-->
      <!-- <script src="main.js"></script> -->
      <script type="text/javascript" src="https://cdn.datatables.net/v/bs4-4.6.0/jq-3.3.1/dt-1.11.0/af-2.3.7/date-1.1.1/r-2.2.9/rg-1.1.3/datatables.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" referrerpolicy="no-referrer"></script>
   </head>
   <body>
      <div class="container">
         <table id="example" class="table table-striped table-bordered" style="width:100%">
            <thead class="thead-light">
               <tr>
                  <th>Something</th>
                  <th>Date</th>
               </tr>
            </thead>
         </table>
      </div>
   </body>
   </html>