Calculate the Number of Days In a Month
Using a combination of date functions you can calculate the number of days in a given month. The rules are as follows (where 'the date' is an input attribute and all other underlined attributes are inferred):
the number of days in the month = DayDifference(the start of the month, the start of the next month)
the start of the month =MakeDate(the year, the month, 1)
the start of the next month = AddMonths(the start of the month, 1)
the year = ExtractYear(the date)
the month = ExtractMonth(the date)
Using these rules, if 'the date' is 2019-06-05, then 'the number of days in the month' would be calculated to be 30.
Tip: To try these rules for yourself, simply copy and paste the rules above into a Word rules document, style them as conclusions, validate the rules and then debug.
The way that these rules work in combination is as follows.
Firstly, you get the first day of the month. To do this you:
- Use the Make Date function to form the date that is 'the start of the month'. In this rule you use a 1 for the day component (that is, the first of the month), the month component of the input date and the year component of the input date:
- the start of the month =MakeDate(the year, the month, 1)
- Use the Extract Month function to get the month component ('the month') of the input date ('the date'):
- the month = ExtractMonth(the date)
- Use the Extract Year function to get the year component ('the year') of the input date ('the date'):
- the year = ExtractYear(the date)
Secondly, you add a month to the first day of the month to get the first day of the next month. To do this you:
- Use the Add Months function to add 1 (month) to 'the start of the month':
- the start of the next month = AddMonths(the start of the month, 1)
Lastly, get the number of days between the first day of the month and the first day of the next month. To do this you:
- Use the Day Difference function to calculate the number of days between 'the start of the month' date and 'the start of the next month' date:
- the number of days in the month = DayDifference(the start of the month, the start of the next month)