Applying moment formatting when the column date is appended to another column
Applying moment formatting when the column date is appended to another column
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?
This question has an accepted answers - jump to answer
Answers
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 valuessort
andtype
. Then if it issort
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 thetype
request returnnum
(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
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.