datetime - conditional datetime formatting using the plugin functionality

datetime - conditional datetime formatting using the plugin functionality

msand01msand01 Posts: 54Questions: 24Answers: 1

Is there a way to do conditional datetime formatting using the plugin functionality?

The following works fine, so I know my 'from' format specifier is correct, except the null date values are transformed as "Invalid date":

{ "data": "UserDate", "render": $.fn.dataTable.render.moment( 'YYYY-MM-DDT00:00:00', 'YYYY-MM-DD' ) }    

To try and amend it to account for null dates, I have tried this:

 { "data": "UserDate", "render": function ( data, type, full, meta ) {
    return (data === null) ? data : $.fn.dataTable.render.moment( 'YYYY-MM-DDT00:00:00', 'YYYY-MM-DD' );}
  },

Now the null values are fine, but I get "Invalid date" for the else portion of the conditional.

What syntax should I be using?

Thank you

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 48,301Questions: 1Answers: 6,970 Site admin
    Answer ✓

    Try:

    var renderer = $.fn.dataTable.render.moment( 'YYYY-MM-DDT00:00:00', 'YYYY-MM-DD' );
    
    ...
    { "data": "UserDate", "render": function ( data, type, full, meta ) {
       return (data === null) ? data : renderer( data, type, row );}
     },
    

    I haven't actually tried it, but I think that should work - although I haven't actually tested it.

    Having said that, I think a better option would be to simply modify the plug-in to support null values. Pull requests for that improvement welcome ;-).

    Allan

Sign In or Register to comment.