【问题标题】:how to format date in jQuery DataTables如何在 jQuery DataTables 中格式化日期
【发布时间】:2020-06-18 17:37:40
【问题描述】:

我正在使用jQuery DataTables,并且我有多个带有日期的列,当前数据采用这种格式2020-06-18 14:32:45.707,我想将其格式化并显示为18/06/2020 14.32

我在 DataTables 中应用了datetime 插件,但仍然无法使用。

目前我正在使用:

render: function(data) {
  return moment(data).format('DD/MM/YYYY HH:mm');
}

哪个工作正常。但我想使用渲染:

render: $.fn.dataTable.render.moment('DD/MM/YYYY HH:mm')

正如documentation 所说,我已将moment.jsdatetime.js 包括在内,我应该申请:

$.fn.dataTable.render.moment(to);

当我使用此方法时,我的日期在我的表格中显示为“无效日期”。 下面是一个演示。

你能解释一下我做错了什么吗?:

$.fn.dataTable.render.moment('DD/MM/YYYY HH:mm')

我有另一种方法,但我想从我的错误中吸取教训,因为我花了很多时间调查并且无法找出问题所在。非常感谢。

$(document).ready(function() {
  $('#example').DataTable({
    "columnDefs": [{
      //render: $.fn.dataTable.render.moment( 'DD/MM/YYYY HH:mm' )
      "render": function(data) {
        return moment(data).format('DD/MM/YYYY HH:mm');
      },
      "targets": 1
    }]
  });
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>
<script src="https://cdn.datatables.net/plug-ins/1.10.21/dataRender/datetime.js"></script>

<table id="example" class="table table-bordered" style="width:100%">
  <thead>
    <tr>
      <th>date before format</th>
      <th>date after format</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>2020-06-18 14:32:45.707</td>
      <td>2020-06-18 14:32:45.707</td>
    </tr>
  </tbody>
</table>

【问题讨论】:

  • 我不认为你可以在不提供回调的情况下内联函数?
  • 如果您只使用这个$.fn.dataTable.render.moment(to);,那么它假定“开始”日期已经格式化为 ISO 8601 日期时间。但是您的源数据是这样的:2020-06-18 14:32:45.707 - 这不是 ISO 8601 日期时间(日期和时间之间没有 T)。所以请改用$.fn.dataTable.render.moment( from, to ); 表单。

标签: javascript jquery datatables momentjs


【解决方案1】:

您的问题在这里:

// Argument shifting
if (arguments.length === 1) {
  locale = 'en';
  to = from;
  from = 'YYYY-MM-DD';
}

默认 FROM 为'YYYY-MM-DD',您需要指定源格式。

const FROM_PATTERN = 'YYYY-MM-DD HH:mm:ss.SSS';
const TO_PATTERN   = 'DD/MM/YYYY HH:mm';

$(document).ready(function() {
  $('#example').DataTable({
    columnDefs: [{
      render: $.fn.dataTable.render.moment(FROM_PATTERN, TO_PATTERN),
      targets: 1
    }]
  });
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>
<script src="https://cdn.datatables.net/plug-ins/1.10.21/dataRender/datetime.js"></script>

<table id="example" class="table table-bordered" style="width:100%">
  <thead>
    <tr>
      <th>date before format</th>
      <th>date after format</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>2020-06-18 14:32:45.707</td>
      <td>2020-06-18 14:32:45.707</td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 非常感谢。我现在明白了这个问题。非常感谢。
  • 如果日期是microsoft json date /Date(1515351600000)/,我们如何设置FROM_PATTERN
  • @Willy M$ 日期已过时 您需要在格式化之前对日期进行预处理。见:stackoverflow.com/a/2316066
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 2015-09-21
  • 2014-10-29
  • 1970-01-01
  • 2011-09-20
  • 2016-05-11
  • 2013-08-02
  • 1970-01-01
相关资源
最近更新 更多