Can You Set The Date Format (datetime displayFormat) For The Whole Table
Can You Set The Date Format (datetime displayFormat) For The Whole Table
woodsalexj
Posts: 21Questions: 6Answers: 0
Is there a way to do something like this?
table = $('#example').DataTable();
table.SetDateFormat**('MM/DD/YYYY');
If there isn't there should be
I can't do it in columndefs etc, because my columns are dynamic (I use the same table definition for multiple pages/tables)
i.e. Column 1 could be a string in one table and a date in the next
This question has an accepted answers - jump to answer
Answers
I think it will have to be done as each cell is displayed. Check if its a date, if it is display it in the format 'MM/DD/YYYY'. I'm just not sure how to do that.
Something like below, won't work, because I don't know how many columns my table will have (sometimes its 5 and sometimes its 10)
$(document).ready(function() {
$('#example').DataTable( {
data: dataSet,
columns: [
{ title: "Name" },
{ title: "Position" },
{
title: "Hire Date.",
render: function (data, type, row) {
//javascript to format the date goes here
}
},
]
} );
} );
Is there something in the data that you are suing to build the columns indicates if its a date column?
One option is to use
columnDefs
with thecolumnDefs.targets
set to_all
to apply to all columns. Depending on your needs you may want to use orthogonal data to apply the formatting for certain functions likedisplay
.Maybe you can post what you are doing to build the columns and we can help with a more efficient solution. One thing you might be able to do is to apply a datetime render for the date columns while you are building them.
Kevin
well other than what I said:
1.) I don't know which column will be the date
2.) I don't know how many columns there will be
The only other thing is Datatables based requirements. For searchbuilder and sorting to work OOB, you have to set the initial date to the format YYYY-MM-DD.
But then when it comes to displaying it to the user I want MM/DD/YYYY format
Something like this?
columnDefs:
[
{
targets: _all,
render: function goes here
{
detect if the data is a date, if it is display in the format 'MM/DD/YY'
else display as is
}
},
],
Here is an example with
targets: "_all"
using moment.js:http://live.datatables.net/huyexejo/1389/edit
Kevin
Option number 2, if you can determine if the column is a Date column, you can move the render function to the code where you build the columns. Something like this:
http://live.datatables.net/gixamihu/1/edit
Not sure if this is helpful as you still haven't shown us the process you are using to dynamically build the columns.
Kevin
full solution in case anyone else needs it:
//Datatables recognizes dates in the format YYYY-MM-DD, so below detects if its a date field and if so converts the format
Nice one - thanks for posting your solution.
It might be worth considering letting moment decide the locale format for you. If all your customers are in the US, then what you have will work nicely, but for international folks (I'm in Scotland ) you could have it display in a locale specific format automatically.
Allan