Calculate number of nights between two dates

Calculate number of nights between two dates

N-tronelN-tronel Posts: 7Questions: 4Answers: 0
edited September 2020 in Free community support

Hi,

I'm working on an accomodation table and I would like to autofill by calculation the number of nights between the check-in date and the check-out date.

I've done that but I've got an error. Can somebody help me please ?

                     {
            "data": "check_in_date"
        },

        {
            "data": "check_out_date"
        },

        {
            "data": "number_of_nights",
            render: function ( data, type, row ) {
                var date1 = New Date (row["check_in_date"]);
     var date2 = New Date (row["check_out_date"]);
     var time_diff = date2.getTime() - date1.getTime();
     var days_Diff = time_diff / (1000 * 3600 * 24);
    return (days_Diff);
}

Thanks for your help

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    I've got an error.

    How about telling us what the error says?

  • N-tronelN-tronel Posts: 7Questions: 4Answers: 0

    Sorry, my table just keep loading and does not display data

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    edited September 2020 Answer ✓

    Sorry, my table just keep loading and does not display data

    Look in your browser's console for errors. There isn't enough information in the code snippet to help debug. Please post a link to your page or a test case replicating the issue so we can take look.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • N-tronelN-tronel Posts: 7Questions: 4Answers: 0
    edited September 2020

    Sorry,

    I just did a little mistake on my code, I wrote New (on "New date") instead of new .

    Thank you for your time and your help.

    Here is the right code, maybe it could help someone some day :

         {
            "data": "check_in_date"
        },
    
        {
            "data": "check_out_date"
        },
    
        {
            "data": "number_of_nights",
            render: function ( data, type, row ) {
                var date1 = new Date (row["check_in_date"]);
     var date2 = new Date (row["check_out_date"]);
     var time_diff = date2.getTime() - date1.getTime();
     var days_Diff = time_diff / (1000 * 3600 * 24);
    return (days_Diff);
    

    }

This discussion has been closed.