Invalid date with datetime-moment.js

Invalid date with datetime-moment.js

RappiRappi Posts: 82Questions: 18Answers: 1

Hi.

I have a databasefield with "YYYY-MM-DD" format.

In my js I have this:

$(document).ready(function() {
    
$.fn.dataTable.moment( 'DD.MM.YYYY' );

and in the data this:

data: "tm_mm_mitglieder.Geburtsdatum",
render: function ( data, type, row ) {
           return (moment(data).format("DD.MM.YYYY"));
    }

All is working but when the date in the database is empty or "0000-00-00" the table show "Invalid date" and the sorting is not working.

Then I have played with this:

data: "tm_mm_mitglieder.Geburtsdatum",
render: function ( data, type, row ) {
    var rowvalueallday = row["5"];
                    
         if (rowvalueallday == '0000-00-00') {
         var gdat = '1900-01-01';
                       return gdat;
                      } else {
                         return (moment(data).format("DD.MM.YYYY"));
                      }

But it's not working.

How can I make the sort working correct and show "00.00.0000" or "Kein Datum angegeben" in the table?

Rappi

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi Rappi,

    I was going to suggest basically what you have done - to an if condition to see if the date is valid or not. If that isn't working, I would need a link to a page showing that issue so I can debug it.

    Thanks,
    Allan

  • RappiRappi Posts: 82Questions: 18Answers: 1

    Hi Allan.

    It's not an open system.
    You can insert this URL

    http://www.rappi.de/Tier-Management/modules/Mitglieds-Management/mitglieder.php

    login with demo/demo and insert the URL again.

    Than you See the Problem in the col "Geburtsdatum".

    Rappi

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The error is var rowvalueallday = row["5"];. Just use the data parameter passed in since that already points to the tm_mm_mitglieder.Geburtsdatum data!

    The issue with row['5'] is that there is no 5 data property in your data source object.

    Allan

  • RappiRappi Posts: 82Questions: 18Answers: 1

    Thanks for your answer.

    Now I have

    data: "tm_mm_mitglieder.Geburtsdatum",
                    render: function ( data, type, row ) {
                        var rowvalueallday =row["tm_mm_mitglieder.Geburtsdatum"];
                        
                          if (rowvalueallday == '0000-00-00') {
                            var gdat = '1900-01-01';
                            return gdat;
                          } else {
                            return (moment(data).format("DD.MM.YYYY"));
                          }       
                           
                    }
    

    Same error :-(

    I think, the moment convert the data before gdat returns and have a problem with a date like 0000-00-00.

    Another idea?

    Rappi

  • RappiRappi Posts: 82Questions: 18Answers: 1

    I have played with but no solution found :-(

    data: "tm_mm_mitglieder.Geburtsdatum",
                    render: function ( data, type, row ) {
                        var rowvalueallday = row['tm_mm_mitglieder.Geburtsdatum'];
                        
                          if (rowvalueallday == '0000-00-00') {
                            var gdat = '1900-01-01';
                            return (moment(gdat).format("DD.MM.YYYY"));
                          } else {
                            return (moment(data).format("DD.MM.YYYY"));
                          }       
                           
                    }
    

    Now I don't have an idea.....

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    row['tm_mm_mitglieder.Geburtsdatum'];

    That is not correct. It is a Javascript object, so you would use:

    row.tm_mm_mitglieder.Geburtsdatum;
    

    or just use the data parameter as I suggested above.

    Allan

  • RappiRappi Posts: 82Questions: 18Answers: 1

    You are my hero (again) :-D

    Thanks!

    PHP is my world but JS ...... Umpff

    Rappi

  • WatcharaWatchara Posts: 30Questions: 5Answers: 1
    edited September 2016

    I had followed by your solution it working to render date time.

    but it's not working to sorting date.

    What should I do? or Mr.Rappi Could you please help me?

    Watchara.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    @Watchara - Please link to a test case showing the issue.

This discussion has been closed.