Can You Set The Date Format (datetime displayFormat) For The Whole Table

Can You Set The Date Format (datetime displayFormat) For The Whole Table

woodsalexjwoodsalexj 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

  • woodsalexjwoodsalexj Posts: 21Questions: 6Answers: 0

    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.

  • woodsalexjwoodsalexj Posts: 21Questions: 6Answers: 0

    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
    }
    },
    ]
    } );
    } );

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    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 the columnDefs.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 like display.

    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

  • woodsalexjwoodsalexj Posts: 21Questions: 6Answers: 0

    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

  • woodsalexjwoodsalexj Posts: 21Questions: 6Answers: 0

    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
    }
    },
    ],

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    Here is an example with targets: "_all" using moment.js:
    http://live.datatables.net/huyexejo/1389/edit

    Kevin

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited September 2022

    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

  • woodsalexjwoodsalexj Posts: 21Questions: 6Answers: 0
    edited September 2022

    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

                        columnDefs: 
                        [
                          {
                            targets: '_all',
                            render: function (data, type, row) {
                              if (type === 'display') {
                                if(isNaN(data) && moment(data, 'YYYY-MM-DD', true).isValid())
                                {
                                    return moment(data).format('MM/DD/YYYY');
                                }
                              }
                              return data;
                            }
                          }
                        ],
    

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin

    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

Sign In or Register to comment.