Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Required Function module to calculate number of days in a year(which includes leap year)

former_member230486
Contributor
0 Kudos

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).

1 ACCEPTED SOLUTION

matt
Active Contributor

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 ). 
7 REPLIES 7

raymond_giuseppi
Active Contributor

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)

matt
Active Contributor

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 ). 

matt
Active Contributor
0 Kudos

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.

0 Kudos

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

0 Kudos

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.

I agree, but I would make one change to your code being.

number_of_days = end_of_year - start_of_year + 1.

Matthew, there seems to be no incentive to reading and exploring documentation when sites like this provide effortless answers.