SAP Convert date and time to timestamp and vice versa

Convert date and time to timestamp and vice versa







  


*& Report ZTIMEZONE_UPDATE
 *&---------------------------------------------------------------------*
 *& Date time conversion between local time, UTC time can be confusing

*& but often the hardest part can be getting the values into the correct format

*& ( i.e. date + time or full timestamp ) and knowing if it has converted *& to UTC or localtime *& *&---------------------------------------------------------------------*

*& Code to help you convert the date format between separate DATE TIME and

*& DATE TIMEStamp *&---------------------------------------------------------------------* REPORT zdate_time_format. DATA: ld_tstamp TYPE tzonref-tstamps. DATA: ld_date TYPE datum, ld_time TYPE uzeit ld_tzone TYPE tzonref-tzone. *Converts a date time into DATE TIMESTAMP * UTC reference means it will convert from UTC to UTC CONVERT DATE ld_date TIME ld_time INTO TIME STAMP ld_tstamp TIME ZONE 'UTC'. * Converts date time (assumed UTC) into date stamp

* based on I TZONE converts into UTC so no change in time just format

* It might not actually be in UTC time but adding UTC means no conversion is done because * because FM assume input is UTC. CALL FUNCTION 'IB_CONVERT_INTO_TIMESTAMP' EXPORTING i_datlo = ld_date i_timlo = ld_time i_tzone = 'UTC' IMPORTING e_timestamp = ld_tstamp. *Does not Convert TIMESTAMP from local tzone to UTC *Actually works the other way round.. see below examples *CONVERT TIME STAMP ld_tstamp TIME ZONE sy-zonlo INTO DATE ld_date TIME ld_time. *Converts date time in local/specified timezone(i.e. GMTUK) to UTC *Does not seem logical as the from tz is on the right but hey ld_tzone = 'GMTUK'. "or use sy-zonlo for local time zone CONVERT DATE ld_date TIME ld_time INTO TIME STAMP ld_tstamp TIME ZONE ld_tzone. *Convert timestamp in UTC time into local/specified time (i.e. GMTUK or EST) *Again seems the wrong way round but seems to work *convert UTC timestamp to GMTUK ld_tzone = 'GMTUK'. "or use sy-zonlo for local time zone CONVERT TIME STAMP ld_tstamp TIME ZONE ld_tzone INTO DATE ld_date TIME ld_time. *Convert UTC timestamp to EST ld_tzone = 'EST'. "or use sy-zonlo for local time zone CONVERT TIME STAMP ld_tstamp TIME ZONE ld_tzone INTO DATE ld_date TIME ld_time. * if you want to Convert timestamp into date time but keep same time simply use UTC as zone

* UTC as the result side, ensures no conversion, just format change

CONVERT TIME STAMP ld_tstamp TIME ZONE 'UTC' INTO DATE ld_date TIME ld_time. *Other functions to to convert Timestamp into DATE TIME CALL FUNCTION 'IB_CONVERT_FROM_TIMESTAMP' EXPORTING i_timestamp = ld_tstamp i_tzone = sy-zonlo IMPORTING e_datlo = ld_date e_timlo = ld_time. CALL FUNCTION 'ISU_DATE_TIME_CONVERT_TIMEZONE' EXPORTING x_date_utc = ld_date x_time_utc = ld_time x_timezone = sy-zonlo IMPORTING y_date_lcl = ld_date y_time_lcl = ld_time EXCEPTIONS general_fault = 1 OTHERS = 2.





Comments on this SAP object

What made you want to lookup this SAP object? Please tell us what you were looking for and anything you would like to be included on this page!