Applying moment formatting when the column date is appended to another column

Applying moment formatting when the column date is appended to another column

silkspinsilkspin Posts: 152Questions: 34Answers: 5

I normally don't have a problem with formatting dates in a column. In cases like that I would use...

columns: [{
    data: "date",
    name: "date",
    render: $.fn.dataTable.render.moment('DD/MM/YYYY', 'MMMM')
}]

However, I'm not sure how to achieve this when I'm trying to manipulate the date value from a column which I append to the value in another column. I've done a simple test case where I would like to add the month as text, and append to the value in the 'Name' column. Eg. I would like this to result in 'Month: August'. I think I'm close but maybe I am approaching this incorrectly?

https://live.datatables.net/tobamole/1/edit

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,237Questions: 1Answers: 10,418 Site admin
    Answer ✓

    Yes, its a bit trickery that way since the rendering function is already effectively "taken up" by the custom function.

    At the moment, what you need to do is check type for being the values sort and type. Then if it is sort data that DataTables is asking for you need to return a sortable value (i.e. convert the month to an integer - January = 0, etc). You could use moment to do that. And that the type request return num (i.e. number sort). More generally, you could use Moment to convert the formatted date to a Unix time stamp number.

    It isn't ideal I realise - I am planning on doing some work to allow multiple formatters (although things might get even more complicated that way!).

    Allan

  • silkspinsilkspin Posts: 152Questions: 34Answers: 5

    Thanks @allan. The example I used was actually simplified to just use the month, but I'd probably need to expand on that which would complicate things. I was sort of guessing that this might be a one time only re-formatting.

    I think making the source the correct format to start with will be the best option. The date column will be hidden and the value appended elsewhere. I was just trying to reuse the same values, but present in different formats.

Sign In or Register to comment.