Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in COBOL

🎈 🎈 🎈
+1

    IDENTIFICATION DIVISION.
    PROGRAM-ID. DateToUnixTimestamp.

    DATA DIVISION.
    WORKING-STORAGE SECTION.
    01  WS-DATE                  PIC X(8) VALUE '20240828'.
    01  WS-TIME                  PIC X(6) VALUE '123456'.
    01  UNIX-TIMESTAMP           PIC 9(10).
    01  EPOCH-DATE               PIC X(8) VALUE '19700101'.
    01  DAYS-SINCE-EPOCH         PIC 9(10).
    01  SECONDS-SINCE-MIDNIGHT   PIC 9(10).

    PROCEDURE DIVISION.
        COMPUTE DAYS-SINCE-EPOCH = FUNCTION INTEGER-OF-DATE(WS-DATE) 
                                    - FUNCTION INTEGER-OF-DATE(EPOCH-DATE).
        
        COMPUTE SECONDS-SINCE-MIDNIGHT = (FUNCTION NUMVAL(WS-TIME(1:2)) * 3600)
                                        + (FUNCTION NUMVAL(WS-TIME(3:4)) * 60)
                                        + FUNCTION NUMVAL(WS-TIME(5:6)).
        
        COMPUTE UNIX-TIMESTAMP = (DAYS-SINCE-EPOCH * 86400) + SECONDS-SINCE-MIDNIGHT.

        DISPLAY 'Unix Timestamp: ' UNIX-TIMESTAMP.

        STOP RUN.     
        

Output:
Unix Timestamp: 1724848496
Example only. There may be multiple ways to perform this operation.

Code Explanation

Converting a date to a Unix timestamp in Cobol is done by first determining the number of days between the given date and the epoch using the FUNCTION INTEGER-OF-DATE, then converting those days into seconds by multiplying by 86400 (the number of seconds in a day).
The time of day is converted into seconds and added to this total. The resulting value is the Unix timestamp, representing the total number of seconds since the epoch for the specified date and time.


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