最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to format date in jQuery DataTables - Stack Overflow

programmeradmin1浏览0评论

I am using jQuery DataTables and I have multiple columns with dates, the current data is in this format 2020-06-18 14:32:45.707 and I want to format it and display it as 18/06/2020 14.32.

I applied datetime plugin in DataTables, but still can't make it work.

Currently I am using :

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

Which is working fine. But I want to use render:

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

I have included moment.js and datetime.js as the documentation says and I should apply:

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

My dates are shown as 'invalid date' in my table when i use this method. below is a demo.

Could you please explain me what am I doing wrong with?:

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

I have the other method working, but I want to learn from my mistakes as I spend much time investigating and couldn't figure out the issue. Thank you very much.

$(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=".1.3/css/bootstrap.css" rel="stylesheet" />
<link href=".10.21/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
<script src=".5.1.js"></script>
<script src=".10.21/js/jquery.dataTables.min.js"></script>
<script src=".10.21/js/dataTables.bootstrap4.min.js"></script>
<script src=".js/2.26.0/moment.min.js"></script>
<script src=".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>

I am using jQuery DataTables and I have multiple columns with dates, the current data is in this format 2020-06-18 14:32:45.707 and I want to format it and display it as 18/06/2020 14.32.

I applied datetime plugin in DataTables, but still can't make it work.

Currently I am using :

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

Which is working fine. But I want to use render:

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

I have included moment.js and datetime.js as the documentation says and I should apply:

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

My dates are shown as 'invalid date' in my table when i use this method. below is a demo.

Could you please explain me what am I doing wrong with?:

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

I have the other method working, but I want to learn from my mistakes as I spend much time investigating and couldn't figure out the issue. Thank you very much.

$(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./ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet" />
<link href="https://cdn.datatables/1.10.21/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
<script src="https://code.jquery./jquery-3.5.1.js"></script>
<script src="https://cdn.datatables/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables/1.10.21/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.26.0/moment.min.js"></script>
<script src="https://cdn.datatables/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>

Share Improve this question edited Jun 18, 2020 at 17:47 Mr. Polywhirl 48.8k12 gold badges93 silver badges144 bronze badges asked Jun 18, 2020 at 17:37 DevTNDevTN 5932 gold badges13 silver badges37 bronze badges 2
  • 1 I do not think you can in-line the function without providing a callback? – Mr. Polywhirl Commented Jun 18, 2020 at 17:44
  • If you only use this $.fn.dataTable.render.moment(to);, then it assumes the "from" date is already formatted as an ISO 8601 datetime. But your source data is this: 2020-06-18 14:32:45.707 - which is not an ISO 8601 date time (there is no T between the date and the time). So use the $.fn.dataTable.render.moment( from, to ); form instead. – andrewJames Commented Jun 18, 2020 at 17:47
Add a ment  | 

2 Answers 2

Reset to default 4

Your issue is here:

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

The default FROM is 'YYYY-MM-DD', you need to specify YOUR source format.

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./ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet" />
<link href="https://cdn.datatables/1.10.21/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
<script src="https://code.jquery./jquery-3.5.1.js"></script>
<script src="https://cdn.datatables/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables/1.10.21/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.26.0/moment.min.js"></script>
<script src="https://cdn.datatables/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>

I have 2 columns, the following code working fine.

$(document).ready(function() {
  $('#example').DataTable({
    columnDefs: [{
      targets: [1,2], render: DataTable.render.datetime('M/DD/YYYY')
    }]
  });
});
发布评论

评论列表(0)

  1. 暂无评论