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

SAP week - Finding dates

Former Member
0 Likes
2,417

I have SAP week number input in my report. From that, how can we get the start date and end date for that week. Can anybody tell me the table where the details of sapweek is getting stored .

19 REPLIES 19
Read only

Former Member
0 Likes
2,179

Use FM : <b>WEEK_GET_FIRST_DAY</b>

To get end date, I could not find any FM, but this is what you can do,

Subtract 1 day from first day

Use <b>DATE_CONVERT_TO_FACTORYDATE</b> , and use <b>CORRECT_OPTION</b> as '-' to get last day of previous week. You need to put factory calendar as well. For plant-specific, factory calendar, you can get it from Table <b>T001W</b>.

Regards,

Subramanian V.

Message was edited by: Subramanian Venkateswaran

Read only

0 Likes
2,179

okay .. what i need is nothing but to subtract 13 weeks from the user input.

ie. suppose user inputs "200452" as sap week .. i need "200439" which is 13 weeks less than the input. HOw can i do that ?

Read only

0 Likes
2,179

p_user = '200452'.
data: lv_variable_integer type i.
data: lv_variable_character(2) type c.

lv_variable_integer = p_user+4(2).
lv_variable_integer = lv_variable_integer - 13.

lv_variable_character = lv_variable_integer.
p_user+4(2) = lv_variable_character.

Regards,

Subramanian V.

Read only

0 Likes
2,179

But if the input is '200502' , it will create problem .. Isn't it ... If i am not wrong, i think it willnot return '2004**' ,Instead it will show error.

Am I right ?

Read only

0 Likes
2,179

Right, oversight.

Probably you can try this.

From FM <b>WEEK_GET_FIRST_DAY</b>, get value of day , in say lv_date.


lv_date = lv_date - 91. " 13 * 7 = 91

Now call FM <b>DATE_GET_WEEK</b>, to get the week number.

Regards,

Subramanian V.

Read only

0 Likes
2,179

A small correction to Subramaniam's code. This takes care of inputs such as 200401 to 200413.


parameters: p_user(6) type c obligatory.

data: lv_year type i,
      lv_week type i.

lv_year = p_user+0(4).
lv_week = p_user+4(2).

subtract 13 from lv_week.

if lv_week le 0.
  add 52 to lv_week.
  subtract 1 from lv_year.
endif.

p_user+0(4) = lv_year.
p_user+4(2) = lv_week.

write p_user.

Thanks and Regards,

Anand Mandalika.

P.S. Of course you will have to take care of the user-input validation in the At-Selection Screen Event. otherwise you are likely to get a short dump.

Read only

0 Likes
2,179

But it will not work sometimes.. ie some years will have 53 weeks whereas some others will have 52 only . Hope u understood the problem ..

Read only

0 Likes
2,179

The 2nd solution that I have stated should work. Why don't you try that out ?

Thanks for letting us know about this 52/53 issue.

Regads,

Subramanian V.

P.S. Just thought that Y2K bug was interesting, but this is even better

Read only

0 Likes
2,179

Hello Shankar,

I do not agree with you completely on this one. No year has 53 <i>Weeks</i>. 52 * 7 = 364. A normal year has 365 days. So there will be one day which will occur 53 times. A leap year has 366 days. So two days will appear 53 times.

Now, if you want to allow for that possibility, that a user can enter the 53rd week as input, then you will also have to consider whether the week would begin on the 365th or the 366th day, as the case may be.

Consider the following example :

1. the week is assumed to begin on a sunday

2. In the year 2000 (which was a leap year), Dec 31st is a Sunday. So a Sunday has occurred 53 times. This can easily be determined by considering the year (first 4 characters of the input) and finding out whether Dec. 31st falls on a Sunday (or on the day on which the week is supposed to begin).

Based on the above logic, you can determine whether the magic number is 52 or 53.

Regards,

Anand Mandalika.

Read only

0 Likes
2,179

Dear Shankar,

Lets Assume input from user in 3 situation

If week 200514.

Extract 14 from 200514 and deduct 13 from it.

you get 200501.

If the deducted value comes to Zero or negative.

Call function BKK_GET_LASTWEEK_OF_YEAR by inputing the pev year which we will get by reducing 1 from input year.

Function retrive Last week of the prev year now reduce rest from that week number.

For Ex:-

input = 200502

year = 2005 week = 02

Normal case.

02 - 13 = -11.

when it becomes -ve

call function which will take input as 2004 which we can get by 2005 - 1.

FM retrieve last week of 2004 as 200453.

Get week part.. 53

Now add -11 to 53 = 42.

concatenate with the year 2004.

I hope this will solve your problem.

Please Let me know

Regards,

Lijo Joseph

Read only

0 Likes
2,179

Hi Poornanand,

I have to differ on your statement, "No year has 53 weeks". Year 2004 has 53 weeks. You can try by calling FM 'DAY_GET_WEEK' and pass 31/12/2004 as date.

Regards,

Subramanian V.

Read only

0 Likes
2,179

Hi Subramaniam,

I had intended a week to be a set of seven days. The normal English meaning. I though this was clear from the explanation I have given.

Coming to your function Module, I assume you meant DATE_GET_WEEK. Executing this Function Module for a set of dates has produced the following results.

31.12.2000 Sunday 200052

31.12.2001 Monday 200201

31.12.2002 Tuesday 200301

31.12.2003 Wednesday 200401

31.12.2009 Thursday 200953

31.12.2004 Friday 200453

31.12.2005 Saturday 200552

I'm yet to find out what this means.

Regards,

Anand Mandalika.

Read only

0 Likes
2,179

Poornanand,

I still work in SY-LANGU = 'E' and a week still has 7 days. What I did is manually count the number of weeks in the year 2004 and the number of weeks are 53.

My colleague had a very good explanation for this.

365 = 52 * 7 + 1

Assume this is a non-leap year.

1st year - Non-Leap Year - Remainder day - 1

2nd year - Non-Leap Year - Remainder day - 1

3rd year - Non-Leap Year - Remainder day - 1

4th year - Leap year - Remainder day - 2

5th year - Non-Leap Year - Remainder day - 1

6th year - Non-Leap Year - Remainder day - 1

Therefore in the sixth year, if you see this accumulation of days has added an entire new week in that year, that is 7 days.

Hope that clears your doubt.

Shankar - Do let us know if the 2nd option that I have provided has worked for you ?

Regards,

Subramanian V.

Read only

0 Likes
2,179

Dear Friends,

As per my knowledge, it is very simple problem.

Reduce 13 from the given week no. if value comes to 0 or -ve add it into last week of the prev year.

to get last week of the year we have FM.

Am i correct?

Regards,

Lijo Joseph

Read only

0 Likes
2,179

Joseph : What you have said is true. The actual problem can be solved very easily. The purpose of this discussion is to get some additional points clarified.

Shankar : Please note that the latter part of this discussion is purely academic and does not concern your actual requirement.

--


This part of the discussion is purely out of academic interest--

Subramaniam: To be honest I'm not very satisfied with your explanation. What you say seems to be making some sense w.r.t. the way SAP defines its calendar and performs the date operations, but I still do not understand how a 53-week year can be detected.

For example, the following are all the years which contain 53-weeks.

2004 2009 2015 2020 2026 2032 2037 2043 2048 2054 2060 2065 2071 2076 2082 2088 2093 2099.

The only observation that I could make is that the difference between two consecutive years above is either 5 or 6.

Mathematically, can you give me some generic formula which will always tell me whether a year has 52 or 53 weeks ?

Regards,

Anand Mandalika.

Read only

0 Likes
2,179

Dear Anand,

Years that start on a Thursday and leap years that start on a Wednesday have 53 weeks.

If still not clear, Pls Let me know.

Regards,

Lijo Joseph

Read only

0 Likes
2,179

Hi Poornanand,

To start with , what seemed like an innocuous post, has turned once again interesting.

Poornanand, weeks are calculated based on ISO 8601:2004 standard. I found this today while searching in Google.

What Lijo has said is true and to support that these are some of the URLs, you would like to visit:

http://www.iso.org/iso/en/prods-services/popstds/datesandtime.html

http://www.personal.ecu.edu/mccartyr/isowdcal.html

http://zijlema.basicguru.com/weeknbr.html

<b>http://personal.ecu.edu/mccartyr/ISOwdALG.txt</b>

http://www.cl.cam.ac.uk/~mgk25/iso-time.html

The URL which is in bold has an algorithm as well.

Regards,

Subramanian V.

Read only

0 Likes
2,179

Hi Joseph and Subramaniam,

Thanks a trillion for the valuable information. I have added a little more to my knowledge repository )

Regards,

Anand Mandalika.

Read only

former_member183804
Active Contributor
0 Likes
2,179

Possibly Fuba 'WEEK_GET_FIRST_DAY' may solve this.

Kind Regards

Klaus