01-23-2019 11:48 AM
Hi Experts,
Is there a SAP FM to calculate number of days in a year which include leap year so that we can take care of number of days in a year ( including leap year).
01-23-2019 12:52 PM
Given the power of the D type in ABAP, I doubt you need this. But who needs a FM anyway?
PARAMETERS year TYPE n LENGTH 4.
DATA(days_in_year) = SWITCH i( CONV numc1( substring( val = CONV d( CONV d( year && '0301' ) - 1 ) off = 7 len = 1 ) )
WHEN '9' THEN 366 ELSE 365 ).
01-23-2019 12:26 PM
Did you search FM with 'YEAR' and 'DAYS' in the name (e.g. HR_MX_DAYS_PER_YEAR ) or just calcualte (TYPE D) first day of year and first day of following year and subtract first date from second date into number of days (TYPE I)
01-23-2019 12:52 PM
Given the power of the D type in ABAP, I doubt you need this. But who needs a FM anyway?
PARAMETERS year TYPE n LENGTH 4.
DATA(days_in_year) = SWITCH i( CONV numc1( substring( val = CONV d( CONV d( year && '0301' ) - 1 ) off = 7 len = 1 ) )
WHEN '9' THEN 366 ELSE 365 ).
01-24-2019 7:24 AM
I should point out that this is rather more complex than the answer I make in a comment below. But the point is, why not think a little about how date types work, and write your own code. This isn't difficult. You only need the FM if you're involving work calendars and suchlike.
01-23-2019 1:26 PM
Use FM " ISU_NUMBER_DAYS_OF_RANGE ", provide inputs as mentioned in below attachment. Based on Number of days in output 365/366 .. you can differentiate between Leap year or not.
or You can use another FM HR_99S_INTERVAL_BETWEEN_DATES
01-24-2019 7:22 AM
This obsession with function modules.
What's wrong with:
DATA(number_of_days) = CONV d( p_year && '1231' ) - CONV d( p_year && '0101' ).
Or in old ABAP
DATA start_of_year TYPE d.
start_of_year = p_year && '0101'.
DATA end_of_year TYPE d.
end_of_year = p_year && '1231'.
DATA number_of_days TYPE i.
number_of_days = end_of_year - start_of_year.
It seems that so many ABAP developers today are unaware how date types work. Sad really.
01-24-2019 10:22 AM
I agree, but I would make one change to your code being.
number_of_days = end_of_year - start_of_year + 1.
01-24-2019 1:08 PM
Matthew, there seems to be no incentive to reading and exploring documentation when sites like this provide effortless answers.