datetime
Convert date / time source data into one suitable for display
- Author: Allan Jardine
- Requires: DataTables 1.10+, Moment.js 1.7+
Date / time formats often from back from server APIs in a format that you don't wish to display to your end users (ISO8601 for example). This rendering helper can be used to transform any source date / time format into something which can be easily understood by your users when reading the table, and also by DataTables for sorting the table.
The MomentJS library is used to accomplish this and you simply need to tell it which format to transfer from, to and specify a locale if required.
This function should be used with the columns.render
configuration
option of DataTables.
It accepts one, two or three parameters:
$.fn.dataTable.render.moment( to );
$.fn.dataTable.render.moment( from, to );
$.fn.dataTable.render.moment( from, to, locale );
Where:
to
- the format that will be displayed to the end userfrom
- the format that is supplied in the data (the default is ISO8601 -YYYY-MM-DD
)locale
- the locale which MomentJS should use - the default isen
(English).
Plug-in code
// UMD
(function( factory ) {
"use strict";
if ( typeof define === 'function' && define.amd ) {
// AMD
define( ['jquery'], function ( $ ) {
return factory( $, window, document );
} );
}
else if ( typeof exports === 'object' ) {
// CommonJS
module.exports = function (root, $) {
if ( ! root ) {
root = window;
}
if ( ! $ ) {
$ = typeof window !== 'undefined' ?
require('jquery') :
require('jquery')( root );
}
return factory( $, root, root.document );
};
}
else {
// Browser
factory( jQuery, window, document );
}
}
(function( $, window, document ) {
$.fn.dataTable.render.moment = function ( from, to, locale ) {
// Argument shifting
if ( arguments.length === 1 ) {
locale = 'en';
to = from;
from = 'YYYY-MM-DD';
}
else if ( arguments.length === 2 ) {
locale = 'en';
}
return function ( d, type, row ) {
if (! d) {
return type === 'sort' || type === 'type' ? 0 : d;
}
var m = window.moment( d, from, locale, true );
// Order and type get a number value from Moment, everything else
// sees the rendered value
return m.format( type === 'sort' || type === 'type' ? 'x' : to );
};
};
}));
CDN
This plug-in is available on the DataTables CDN:
Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.
Version control
If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!
- This plug-in: datetime.js
- Full DataTables plug-ins repository: DataTables/Plugins
Examples
// Convert ISO8601 dates into a simple human readable format
$('#example').DataTable( {
columnDefs: [ {
targets: 1,
render: $.fn.dataTable.render.moment( 'Do MMM YYYY' )
} ]
} );
// Specify a source format - in this case a unix timestamp
$('#example').DataTable( {
columnDefs: [ {
targets: 2,
render: $.fn.dataTable.render.moment( 'X', 'Do MMM YY' )
} ]
} );
// Specify a source format and locale
$('#example').DataTable( {
columnDefs: [ {
targets: 2,
render: $.fn.dataTable.render.moment( 'YYYY/MM/DD', 'Do MMM YY', 'fr' )
} ]
} );