Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Unix Timestamp to Datetime in COBOL

How to convert Unix timestamp to Datetime in COBOL

🎈 🎈 🎈
+1

    IDENTIFICATION DIVISION.
    PROGRAM-ID. ConvertUnixTimestamp.

    DATA DIVISION.
    WORKING-STORAGE SECTION.
    01  UNIX-TIMESTAMP     PIC 9(10) VALUE 1693997245.
    01  EPOCH-DATE         PIC X(8)  VALUE '19700101'.
    01  SECONDS-PER-DAY    PIC 9(10) VALUE 86400.
    01  DAYS-SINCE-EPOCH   PIC 9(10).
    01  REMAINDER-SECONDS  PIC 9(10).
    01  FINAL-DATE         PIC X(10).
    01  FINAL-TIME         PIC X(8).

    PROCEDURE DIVISION.
        COMPUTE DAYS-SINCE-EPOCH = UNIX-TIMESTAMP / SECONDS-PER-DAY.
        COMPUTE REMAINDER-SECONDS = UNIX-TIMESTAMP MOD SECONDS-PER-DAY.
        ADD DAYS-SINCE-EPOCH TO FUNCTION INTEGER-OF-DATE(EPOCH-DATE) GIVING FINAL-DATE.
        MOVE FUNCTION DATE-OF-INTEGER(FINAL-DATE) TO FINAL-DATE.
        MOVE FUNCTION TIME-OF-DAY(SECONDS-PER-DAY * REMAINDER-SECONDS) TO FINAL-TIME.
        DISPLAY 'Converted Date: ' FINAL-DATE ' ' FINAL-TIME.
        STOP RUN.     
        

Output:
Converted Date: 2023-09-06 10:47:25
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this COBOL program we calculate the number of days ('DAYS-SINCE-EPOCH') by dividing the timestamp by the number of seconds in a day ('SECONDS-PER-DAY'). The remainder seconds ('REMAINDER-SECONDS') are then calculated to determine the time of day.
The FUNCTION INTEGER-OF-DATE converts the epoch date into an integer, and ADD DAYS-SINCE-EPOCH calculates the final date by adding the days to the epoch date. The FUNCTION DATE-OF-INTEGER converts this integer back to a date format, and the FUNCTION TIME-OF-DAY is used to derive the exact time from the remainder seconds.


Other useful COBOL date functions

Function Description Example
FUNCTION CURRENT-DATE Returns the current date and time in the format 'YYYYMMDDHHMISS9999', where YYYY is the year, MM is the month, DD is the day, HH is the hour, MI is the minutes, SS is the seconds, and 9999 is the milliseconds. FUNCTION CURRENT-DATE returns 202408281234567890
FUNCTION DATE-OF-INTEGER Converts an integer (days since 1601-01-01) to a date in 'YYYYMMDD' format. FUNCTION DATE-OF-INTEGER(738120) returns 20240828
FUNCTION INTEGER-OF-DATE Converts a date in 'YYYYMMDD' format to an integer representing the number of days since 1601-01-01. FUNCTION INTEGER-OF-DATE(20240828) returns 738120
FUNCTION DAY-OF-WEEK Returns the day of the week (1-7) for a given date in 'YYYYMMDD' format, where 1 is Monday and 7 is Sunday. FUNCTION DAY-OF-WEEK(20240828) returns 3 (Wednesday)
FUNCTION YEAR-TO-YYYY Converts a 2-digit year (YY) to a 4-digit year (YYYY), handling century rollovers. FUNCTION YEAR-TO-YYYY(24) returns 2024
FUNCTION INTEGER-OF-DAY Returns the number of days from 1600-12-31 to a given date in 'YYYYMMDD' format. FUNCTION INTEGER-OF-DAY(20240828) returns 738120
FUNCTION DAY-OF-INTEGER Converts the number of days since 1600-12-31 to a date in 'YYYYMMDD' format. FUNCTION DAY-OF-INTEGER(738120) returns 20240828
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder