### Post

2 followers Follow

# Days booked in a month

Guest Bookings app has the following:

Check In - Check Out

28/02/2019 - 4/3/2019 (1 night in Feb, 3 nights in March)
10/03/2019 - 27/3/2019 (26 nights in March)
27/3/2019 - 15/4/2019 (5 night in March, 14 nights in April

The app called Monthly Reports has a report for each house per month and each report has all its the guest bookings related to it.

How do I calculate the number of nights booked for March only i.e not included the 1 night from Feb nor the 14 nights.

I am ok with basic days diff but how should I incorporate the Month component?

var a = moment(@Check_In_Date);
var b = moment(@Check_Out_Date);

b.diff(a,"d")+1

Isrial Pritchard

### 1 comment

Hi Isrial,

you need a more elaborated code which checks each day (date) in each booking's time range if it is a date in the wanted month.
You must have a variable in the month's calculation field where the wanted month is defined in the format "MMYYYY" (that depends on how your report app is structured e.g. if you've a field where the wanted month/year is entered, if you have one field for each month etc.). The general approach is:

`var month = "032019";var checkIns = @All of Check_In_Date;var checkOuts = @All of Check_Out_Date;var dayCount = 0;for(var i = 0; i < checkIns.length; i++){    var bookingDays = moment(checkOuts[i]).diff(moment(checkIns[i]),"d") + 1;    for(var j = 0; j < bookingDays; j++){        if(moment(checkIns[i]).add(j,"d").format("MMYYYY") == month){          dayCount += 1;        }    }};dayCount`

Rainer