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. *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. *Convert TIMESTAMP from local tzone to UTC CONVERT TIME STAMP ld_tstamp TIME ZONE sy-zonlo INTO DATE ld_date TIME ld_time. *Convert date time in UTC time into local time CALL FUNCTION 'IB_CONVERT_INTO_TIMESTAMP' EXPORTING i_datlo = ld_date i_timlo = ld_time i_tzone = sy-zonlo IMPORTING e_timestamp = ld_tstamp. * Convert timestamp into UTC date time

* UTC on the source 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.