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: 

How to get all the dates in between a range of dates

Former Member
0 Kudos

I want to store all the dates that come in the date range given by user as input. eg:

Date-low = '01-01-2005'

Date-high = '05-01-2005'

Then itab should contain

Date

-


'01-01-2005'

'02-01-2005'

'03-01-2005'

'04-01-2005'

'05-01-2005'

Is there any FM which gives a list of dates that comes between 2 dates ?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Shankara Narayanan T.V

I tried the code of : loop at s_inputdate.

1. first error : cannot be more than 8 char long.

2. secondly its showing only one entry -ie. the low one.

It loops only one time.

Thanks & Regards,

Amit Mittal.

13 REPLIES 13

Former Member
0 Kudos

Hi Dear Shankara Narayanan T.V

1. I have created a sample program for your requirement,

and also tested it. Its working fine.

2. Logic is :

Calculate in a loop using +1.

2. I m new to this sdn.

Please give points if you are satisfied.

Thanks & Regards,

Amit Mittal.

*----


REPORT abc.

*----


DATA : BEGIN OF itab OCCURS 0,

mydate TYPE sy-datum,

END OF itab.

SELECT-OPTIONS budat FOR sy-datum OBLIGATORY.

*----


END-OF-SELECTION.

PERFORM get_all_dates.

LOOP AT itab.

WRITE / itab-mydate.

ENDLOOP.

*----


FORM get_all_dates.

CLEAR itab.

REFRESH itab.

DATA : myctr TYPE i.

DATA : newdate TYPE sy-datum.

newdate = budat-low.

*----


Loop And Generate Dates

DO.

IF newdate <= budat-high.

itab-mydate = newdate.

APPEND itab.

ELSE.

itab-mydate = sy-datum.

EXIT.

ENDIF.

newdate = newdate + 1.

ENDDO.

ENDFORM. "get_all_dates

0 Kudos

Thank you for looking into my problem. But i am really sorry bcoz, I want some shortcut solution. The way u have told is absolutely okay . While posting this, i was using this logic only. I want to change it bcoz its a bit lengthy . Whenever year changes.. that means if the input is

2004-12-12 to 2005-01-01 ...

Your code will not work.

So we have to add 1 day using a function module (Instead of giving newdate = newdate + 1). I know how to do that .But since it becomes complicated, I am asking whether there is any alternative for this ?

0 Kudos

Dear Amit Mital,

There is a really simple way for doing that .

Please note down the following code . It suits the requirement.

tables: mkpf.

select-options:

s_inputdate for mkpf-budat.

Data: begin of it_dates occurs 0,

date like mkpf-budat,

end of it_dates.

start-of-selection.

loop at s_inputdate. " Selection-field

it_dates-date = s_inputdate-low.

append it_dates.

endloop.

0 Kudos

The solution is good. However, in your previous reply, you have mentioned that, if the range falls between two different years, newdate = newdate + 1 will not work. I doubt that.


data: lv_date type d value '20041230'.

lv_date = lv_date + 1.
write lv_date.

lv_date = lv_date + 1.
write lv_date.

lv_date = lv_date + 1.
write lv_date.

The above code works for new year. Let me know if I have not understood you correctly.

Nevertheless, your solution is elegant and much better.

Regards,

Subramanian V.

P.S.- Please mark question as answered

0 Kudos

Dear Subramanian,

What u told is absolutely correct .date = date + 1 works properly . I am sorry for my previous statement. Actually i was thinking about weeks..In the case of dates, it will work properly.. But if suppose our requirement is to list the weeks in between the input range, then the code will not work . I mean if input is week no 200453 to 200501.. problem will occur . I think i am right ? Aren't I ? What i told will work properly for both (I think so) . Please get back to me if u have any other solutions or suggestions..

Thank you.

0 Kudos

I agree with you.

Regards,

Subramanian V.

Former Member
0 Kudos

Hi Shankara Narayanan T.V

I tried the code of : loop at s_inputdate.

1. first error : cannot be more than 8 char long.

2. secondly its showing only one entry -ie. the low one.

It loops only one time.

Thanks & Regards,

Amit Mittal.

0 Kudos

Dear Subramanian ,

As amit has pointed out, my code will not work whenever the user gives a inputdate-low to a inputdate-high. The previous logic given by Amit will work when he gives both low and high values. But of course it will not work for selected entries in input .. I mean if the user gives some specific dates say 01.01.2005, 04.01.2005 , 06.01.2005 like that.. In that case only the code given by me will work . Hope u r catching my point. So, i think a combination of Amit's code and mine will do.

if s_inputdate-high eq space.

paste my code

else.

paste amit's logic.

endif.

This will certainly solve the problem. But i want to know whether this is the proper way ? Any better way ?

Please get back to me if u dont understand what i told.

Thank you .

0 Kudos

Instead of digging into the solution for finding out the range, can you tell us why you need all the possible dates in an internal table?

Perhaps, there is a better solution for that. This is because with select-options, the flexibility is very high.

You can also have not equal to, pattern, excluding range etc...

Regards,

Subramanian V.

0 Kudos

Its extremely difficult for me to explain the situation . The requirement can be satisfied only by getting the dates in an internal table. For explaining the situation , it will take a lots of explanations .. So, i can't . Please dont mind.. I know the flexibility of select-options. But for this requirement , this is mandatory and cannot think of anyother logic. Presently, as per i told in my last reply, its working. If possible, if any better soln comes to ur mind, please let me know. Otherwise dont mind. As of now, its working. Thank you very much all of u .

0 Kudos

Hi Shankar,

Here's a code snippet which I believe will be suitable for your case. However, please note that the code does not handle the case where the user enters Exclusion Ranges and Exclusion Values. I guess you do not need the exclusion tabs of the select-options in your case. You can use the FM SELECT_OPTIONS_RESTRICT to prevent the user from entering them.


tables vbak.

select-options s_erdat for vbak-erdat.

data:  creation_date type d occurs 0 with header line,
       date_count type i.


start-of-selection.

  loop at s_erdat.

    if s_erdat-high is initial.

      append s_erdat-low to creation_date.

    else.

      clear date_count.

      date_count = s_erdat-high - s_erdat-low .
      add 1 to date_count.

      creation_date = s_erdat-low.
      append creation_date.

      do date_count times.
        add 1 to creation_date.
        append creation_date.
      enddo.

    endif.

  endloop.

  sort creation_date.

  loop at creation_date.
    write : / creation_date.
  endloop.

=================================================

I have tried a few cases where I expected the problems due to the years' change and have found that this code overcomes all of them. If you find a flaw, please do let me know.

Regards,

Anand Mandalika.

0 Kudos

Dear all,

Now one more requirement is there .. I need to store all the input materials in an internal table .. For this , I cannot sat matnr = matnr + 1 .. So, what can i do ?

thanking you,

shankar

0 Kudos

Hello Shankar,

Was your previous probelm solved? If yes, then please reward the points to the answers that you have found to be useful.

Now coming to your question, I think it should actually be started in a new thread. Anyways, here' what I think :


tables mara.

select-options: s_matnr for mara-matnr.

data: begin of material occurs 0,
        matnr type matnr,
      end of material.

start-of-selection.
  select matnr 
    from mara
    into table material
   where matnr in s_matnr.

The reasons why I have given the above code :

1. I believe even if there were to be some way of coding and extracting the materials in the given range, it is definitely going to take some time (compared to the time it has taken to code the above SELECT , atlease ) to realize the logic.

2. And even if the logic were to be written, it would definitely take more time to execute the logic than to obtain the materials from the database.

3. Let us say that the user has given a range like A00000000000000000 to A00000000000000999. This is actually the set of all the 1000 materials. Even if you write the logic to get all these 1000 materials, most of the materials might not even exist in your system. Imagine the number of unwanted MATNRs that will get generated for some other range, which can probably take forever to generate the values of MATNRs.

Hope that was convincing enough...

Regards,

Anand Mandalika.