Application Development and Automation 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: 
Read only

Calculate a specific date

OzgurKorkmaz
Explorer
0 Likes
2,075

Hello,

I have a request to calculate certain days of the month. For example, I need to reach dates such as the 2nd Tuesday of the month or the 4th Friday of the month. Is there a program or FM where I can calculate this?

If I need to explain in more detail, when I say the 2nd Tuesday of the month, the date I want to reach is 09.11.2021 according to the calendar below. When I say Friday the 4th it's 26.11.2021

1 ACCEPTED SOLUTION
Read only

Nikhil_Rao_ABAP
Participant
1,968

Hello,

Here is a snippet I built, that I had pasted earlier, but the formatting got muddled up when I edited it...

PARAMETERS:
get_nth TYPE numc2, "01st/02nd/03rd/04th/05th
this_day TYPE numc2, "01 M/02 T/../07 Su
of_month TYPE numc2, "01/02/../12
in_year TYPE gjahr.

START-OF-SELECTION.

DATA:
yearweek_of_month TYPE kweek,
monday_of_nth_week TYPE scdatum,
date_on_nth_this_day_of_month TYPE scdatum.

"CL_SCAL_UTILS=>DATE_GET_WEEK
CALL FUNCTION 'DATE_GET_WEEK'
EXPORTING
date = CONV scdatum( |{ in_year }{ of_month }01| )
IMPORTING
week = yearweek_of_month
EXCEPTIONS
date_invalid = 1
OTHERS = 2.
* Jump to week
yearweek_of_month = yearweek_of_month + get_nth - '01'.

* Monday of this week
"CL_SCAL_UTILS=>WEEK_GET_FIRST_DAY
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
week = yearweek_of_month
IMPORTING
date = monday_of_nth_week
EXCEPTIONS
week_invalid = 1
OTHERS = 2.
date_on_nth_this_day_of_month = monday_of_nth_week + this_day - '01'.

WRITE: date_on_nth_this_day_of_month.

Hello,

I have a request to calculate certain days of the month. For example, I need to reach dates such as the 2nd Tuesday of the month or the 4th Friday of the month. Is there a program or FM where I can calculate this?

If I need to explain in more detail, when I say the 2nd Tuesday of the month, the date I want to reach is 09.11.2021 according to the calendar below. When I say Friday the 4th it's 26.11.2021

3 REPLIES 3
Read only

amontella96
Active Contributor
0 Likes
1,968

Hi ozgur_korkmaz

here?

Read only

Nikhil_Rao_ABAP
Participant
1,969

Hello,

Here is a snippet I built, that I had pasted earlier, but the formatting got muddled up when I edited it...

PARAMETERS:
get_nth TYPE numc2, "01st/02nd/03rd/04th/05th
this_day TYPE numc2, "01 M/02 T/../07 Su
of_month TYPE numc2, "01/02/../12
in_year TYPE gjahr.

START-OF-SELECTION.

DATA:
yearweek_of_month TYPE kweek,
monday_of_nth_week TYPE scdatum,
date_on_nth_this_day_of_month TYPE scdatum.

"CL_SCAL_UTILS=>DATE_GET_WEEK
CALL FUNCTION 'DATE_GET_WEEK'
EXPORTING
date = CONV scdatum( |{ in_year }{ of_month }01| )
IMPORTING
week = yearweek_of_month
EXCEPTIONS
date_invalid = 1
OTHERS = 2.
* Jump to week
yearweek_of_month = yearweek_of_month + get_nth - '01'.

* Monday of this week
"CL_SCAL_UTILS=>WEEK_GET_FIRST_DAY
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
week = yearweek_of_month
IMPORTING
date = monday_of_nth_week
EXCEPTIONS
week_invalid = 1
OTHERS = 2.
date_on_nth_this_day_of_month = monday_of_nth_week + this_day - '01'.

WRITE: date_on_nth_this_day_of_month.
Read only

0 Likes
1,968

Hello Ozgur,

Thanks for the appreciation, just realised that the snippet above may not necessarily yield the exact results, because it depends on the semantics of our question. ( e.g. I tried the 01(st), 02(Tuesday) of 10(Oct) of 2021... and this logic actually returned the 28.09.2021 😞 ... the expected answer is 05.10.2021(although we notice that it is in the second week of Oct)..... but I hope the methods/functions mentioned above can assist you to build a more robust logic. One painful but bulletproof way is reconstruct the month in the internal table( dates & days) and then run your logic on that table..... or tweak around with the math in the logic by finding which day is the 1st of the month and working forwards from it...

Hope this helps more than the snippet did!!