A quick method of determining the number of days in a month in WebIntelligence is through the use of the **LastDayOfMonth** function.

For a specified date, the **LastDayOfMonth** will give you the last day in that month.

**=LastDayOfMonth(CurrentDate())**

To extract the number of days, we want to use the first two digits of this result. The following formula will acheive that:

**=Left(FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”);2)**

We’ve added two functions here. The first is **FormatDate**.** LastDayOfMonth** returns a value held as a date field. We ultimately want the first two digits of this field. However, using the **Left()** function means we have to have a string input, not a date input. We have to convert the input into a string format first.

This requires two steps.

The first is to convert the **LastDayOfMonth** value into a string. We do this using the **FormatDate **function:

**=FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”)**

We now have a string.

We then extract the first two digits to give us the number of days in a month:

**=Left(FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”);2)**

If we want to subsequently use this in a calculation, we need to further manipulate this value and convert it to a number. This is achieved using the **ToNumber** function:

**number ToNumber(string number_string)**

This takes a string value and converts it to a number.

In our case, we create the following formula:

**=ToNumber(Left(FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”);2))**

We now have the number of days for a particular month that we can use in our calculations.

**Missing FirstDayOfMonth() function…**

Sadly, there isn’t a function to determine the first day of the month. However, we have a method of extracting the last day of the month using **LastDayOfMonth()** and using the formulas described above, we can work out the first day:

=**RelativeDate(LastDayOfMonth(CurrentDate());-ToNumber(Left(FormatDate(LastDayOfMonth(CurrentDate()) ****;”dd/MM/yyyy”);2))+1)**

Despite looking complex, we are subtracting the number of days in the month (plus one) from the last day of the month. If we subtracted the total number of days in the month, we would end up with the last day of the previous month! We don’t want this. So we add a 1 to give us the first day.

Also note that in order to subtract days, we have *prefixed* the **ToNumber** function with a minus symbol.

**Update:**

Hat tip to **JB** for pointing out that we can avoid the string conversion by using the **DayNumberOfMonth** function.

Hence, to calculate the last day of the month:

**=DayNumberOfMonth(LastDayOfMonth(CurrentDate()))**

And the first day of the month:

**=DayNumberOfMonth(RelativeDate(CurrentDate(); 1-DayNumberOfMonth(CurrentDate())))**