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: 

Date calculation

Former Member
0 Kudos

Hi Experts,

Is there any FM for date calculation.

I have one scenario

In selection screen we have on Select Option field ‘Date’.

If I put the some ranges in Date (selection field ) , we need to calculate how many dates in between.

Could you please help me.

Thanks and regards,

Amjad Hussain,

1 ACCEPTED SOLUTION

former_member208856
Active Contributor
0 Kudos

Hi,

Please use the code given below :

data : date_diff type p, earliest type C, timediff type p.

data : c_new type t, d_new type t.

selection-screen begin of block b1 with frame.

SELECT-OPTIONS: date FOR sy-datum.

selection-screen end of block b1.

CALL FUNCTION '/SDF/CMO_DATETIME_DIFFERENCE'

EXPORTING

date1 = date-low

time1 = c_new

date2 = date-high

time2 = d_new

IMPORTING

DATEDIFF = date_diff

TIMEDIFF = timediff

EARLIEST = earliest

EXCEPTIONS

INVALID_DATETIME = 1

OTHERS = 2

.

IF sy-subrc <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

write : / date_diff.

I hope, it can help you,

Reward points, if helpful,

Sandeep Kaushik

3 REPLIES 3

former_member208856
Active Contributor
0 Kudos

Hi,

Please use the code given below :

data : date_diff type p, earliest type C, timediff type p.

data : c_new type t, d_new type t.

selection-screen begin of block b1 with frame.

SELECT-OPTIONS: date FOR sy-datum.

selection-screen end of block b1.

CALL FUNCTION '/SDF/CMO_DATETIME_DIFFERENCE'

EXPORTING

date1 = date-low

time1 = c_new

date2 = date-high

time2 = d_new

IMPORTING

DATEDIFF = date_diff

TIMEDIFF = timediff

EARLIEST = earliest

EXCEPTIONS

INVALID_DATETIME = 1

OTHERS = 2

.

IF sy-subrc <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

write : / date_diff.

I hope, it can help you,

Reward points, if helpful,

Sandeep Kaushik

Former Member
0 Kudos

Hi mohammed,

You can use the following FM

CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'

EXPORTING

DATE1 = higher date

DATE2 = lower date

OUTPUT_FORMAT = '02'

IMPORTING

DAYS = W_DAYS

EXCEPTIONS

INVALID_DATES_SPECIFIED = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Based on output_format you can get years/days/months combination

OUTPUT_FORMAT = '02' will give you the days only as requested

Other function modules are also available like

FIMA_DAYS_AND_MONTHS_AND_YEARS

FIMA_DAYS_BETWEEN_TWO_DATES

Reward if helpful

Regards

Byju

Former Member
0 Kudos

Use below FM :

Just use DAY_ATTRIBUTES_GET function module and give DATE_FROM and DATE_TO it it, and it will return an internal table with all the dates in between. It gives you more information than you need, but it is the easiest.

If you use other FM then if date from and date to are in same year ,then it works .

if dates are not in same year,then it will not work.

Try to use DAY_ATTRIBUTES_GET -> it gives the all information.

Thanks

Seshu