[SOLVED] Crystal Reports – ‘A month number must be between 1 and 12’ error

Problem

When attempting to calculate previous month you receive the following error message

A month number must be between 1 and 12

This occurs when the current month is January and the formula has tried to use the current month minus 1.

For example,

month(currentdate) - 1

CrystalReports-MonthNumber1121

Solution

There are ways to resolve this issue, the ‘best’ way depends on what you are trying to achieve.

Using DateSerial

DateSerial is a highly configurable function with lots of options.

The following formula will give you the last day of the last month

DateSerial ((Year (CurrentDate)),(Month (CurrentDate)-1) ,Day (Maximum (LastFullMonth)))

and this formula will give you the first day of the last month

DateSerial ((Year (CurrentDate)),(Month (CurrentDate)-1) ,Day (Minimum (LastFullMonth)))

and this formula will give you the last day of the current month

DateSerial ((Year (CurrentDate)),(Month (CurrentDate)+1) ,1)-1

This solution is far more simple but not as configurable.

In the example above, where

month(currentdate) - 1

created an error message, we would instead use

Month(Maximum(LastFullMonth))

If the current month was January this would return 12