Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Getting day of the year from DD/MM/YYYY using function in SAP system
In SAP systems, you can calculate the day of the year from a DD/MM/YYYY date using a simple function. The day of the year represents which day number it is within that specific year (1-366).
You can achieve this with the following line of code ?
DATA(l_day) = m_date - CONV d(m_date(4) && '0101' ) + 1.
Where m_date is the input date that needs to be of type d. Note that the format of type d is YYYYMMDD.
This code works by:
- Extracting the year from the input date using
m_date(4) - Concatenating it with '0101' to create January 1st of the same year
- Converting it to date type using
CONV d - Subtracting January 1st from the input date and adding 1
Alternative Method
If the above function is not available in your system, you can use the below code using simple date subtraction ?
DATA: l_date TYPE d,
l_jan_01 TYPE d,
l_day TYPE i.
l_date = "your input date in YYYYMMDD format"
l_jan_01 = l_date.
l_jan_01+4 = '0101'. " sets the date to first day of year
l_day = l_date - l_jan_01 + 1.
In this alternative approach:
-
l_datestores your input date -
l_jan_01is created by copying the input date and modifying positions 4-7 to '0101' - The final calculation subtracts January 1st from the input date and adds 1 to get the day number
Example Output
For a date like 20231215 (December 15, 2023), the result would be ?
349
This means December 15, 2023 is the 349th day of the year.
Conclusion
Both methods effectively calculate the day of the year by finding the difference between the input date and January 1st of the same year. Choose the method that best fits your SAP system version and coding preferences.
