Format of the date depends on various factors, to give you example it can depend on the country, type of business and even on personal choice. Internally SAP stores the date in YYYYMMDD format in char length 8 type field. Storing the date in this format makes it format independent, take less space to store and easy to sort and easy to compare. Although date is stored in ‘YYYYMMDD’ format but it is never displayed to user as it is (unless intended). While displaying the date to user SAP always takes care of date format active for user. Statements WRITE automatically display date field based on user date format. This statement is particularly important if you are using a date in BDC. Always remember to format date using WRITE statement before pass date to screen in BDC mode. A user can change the date format for his/her profile using transaction SU3. Centrally it can change by administrators using SU01/SU10.
Basic addition and subtracts in days work perfectly fine on date type field in SAP. It takes care of number of days in month and leap year. For example, adding 5 to 29-dec-2009 will return 03-Jan-2010. And subtracting 1-Mar-2009 and 27-Feb-2009 will result in 2. Remember you can always determine the year month and day of a date variable by extracting the correct characters. So if V_DATE is your date variant in SAP then
Year = V_DATE(4).
Month = V_DATE+4(2).
Day = V_DATE+6(2).
It worth noting that table T247 stores the short and Long description of month or you can use function module MONTH_NAMES_GET for the same.
Related Blogs.
First Day and Last Day of Month
Simple Date Formatting
FM ADD_TIME_TO_DATE
List of All Blogs
-
►
2008
(39)
-
►
July
(9)
- Display ListBox in Parameter of Selection Screen
- Important EDI Transactions and Programs
- Hide F8 Button on Selection Screen
- Find Obsolete Function Module
- Sample RS_COVERPAGE_SELECTIONS
- SAP ABAP Dynamic Internal table and Processing 02
- SAP ABAP Dynamic Internal table and Processing 01
- Smartform/Sapscript Page Counter - Mode
- SAP SD Pricing Overview
-
►
June
(12)
- Transporting Table Entry
- Sample/Demo code availble in SAP
- SELECT-OPTION in Module Pool Screen
- Add your own pattern in SE38
- Call Transaction in New Window
- Minimum Code Required to Send SAP Mail
- Calculate Tax of Purchase order Line Item
- Send report as attachment in background
- Button on Selection-Screen
- Displaying Two ALV Grid on Screen
- Function Module for getting information of Diction...
- HR ABAP 02 - Getting Started - LDB PNP
-
►
July
(9)
Useful Links
Categories
Abap Objects
ALV List
Config
Dynamic
EDI
Excel
Formatting
HR ABAP
MD04
Module Pool
OLE Automation
Open Dialog Box
Quick Reference
Report
Sample Code
SAP ABAP
SAP FI
SAP Mail
SAP MM
SAP Notes
SAP SD
Sapscript
Selection-Screen
Smartforms
Standard Text
Translation
Tutorial
Upload Download
Utility
Web service
xml to ABAP
XSLT
You are most welcome to use any information available in this blog. Any usage of the information or sample code is at your own risk. I do take care of accuracy and relevance of blog before publishing. However, I do not guarantee that information and code are accurate and bug free, their can be alternate and better way of doing same thing so please use information as reference only. Relevance of code and instructions can possibly change with time, check the blog post date before using this information. This is my personal blog, recommendation and opinions expressed in this blog are mine and not of any company of any of my employer.
I do moderate comments and publish as soon as I check them. You can post questions as well, which I will try best to solve. I will anyway publish your questions in case others readers might have answer.
Thanks for coming here and reading the blogs :)
I do moderate comments and publish as soon as I check them. You can post questions as well, which I will try best to solve. I will anyway publish your questions in case others readers might have answer.
Thanks for coming here and reading the blogs :)
Sunday, September 27, 2009
First and Last Day of the Month
Getting first day of the month is easy as first day of month is always 01, so you can replace the day (DD) part of the date field with 01 and you are done. However getting the last day of month is but tricky as last can 28, 30 or 31 depending on what month and leap year. Easier way to find the last day of month is subtract 1 from 1 days of next month.
REPORT zpwtest .
DATA : v_date TYPE syst-datum ,
v_first TYPE syst-datum ,
v_last TYPE syst-datum .
v_date = sy-datum .
*First day of month will be
CONCATENATE v_date(4) v_date+4(2) '01' INTO v_first .
WRITE : / 'First day of month : ' , v_first .
*And to find last day, first get a date in next month
IF v_date+4(2) > 15 .
v_date = v_date + 20 .
ELSE.
v_date = v_date + 30 .
ENDIF.
*Then get first day of next month
CONCATENATE v_date(4) v_date+4(2) '01' INTO v_last .
*Subtract one day will be last day of previous month
v_last = v_last - 1 .
WRITE : / 'Last day of month : ' , v_last .
Labels:
Date,
Sample Code,
SAP ABAP
Subscribe to:
Posts (Atom)