In custom developments, occasionally, requirement demands calculation or identification of dates like – 90 days before the current date, first date of the month, months between dates etc. SAP, fortunately, provides many function modules to facilitate such calculations which at times take into account calendars as well which are tricky to handle if an algorithmic calculation is being employed in code.
Some known FMs are ADD_TIME_TO_DATE which adds time to date, DATE_IN_FUTURE which calculates future dates but the leader of the pack are HR* FMs.
- HR_99S_DATE_ADD_SUB_DURATION – adds/subtracts days/months/years to a current date to offer a future date.
- HR_99S_INTERVAL_BETWEEN_DATES – provides the difference between two dates. A similar FM HR_99S_MONTHS_BETWEEN_DATES provides only months between two dates. FM HR_CALC_YEAR_MONTH_DAY also provide similar information.
- HR_E_NUM_OF_DAYS_OF_MONTH – stipulates the number of days of a given month and year taking into account leap year factor
To find HR* FMs for dates, a simple way is to check with HR*DATE or HR*MONTH in SE37 and it will list out a number of FM. Based on the definition and some trial and error, it is easy to find a correct FM.
There are some more FMs for reference –