Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in Prolog

🎈 🎈 🎈
+1

    :- use_module(library(date)).

    % Convert Unix timestamp to Date
    convert_unix_to_date(UnixTimestamp, Date) :-
        stamp_date_time(UnixTimestamp, DateTime, 'UTC'),
        format_time(atom(Date), '%Y-%m-%d %H:%M:%S', DateTime).
    
    % Example usage
    ?- UnixTimestamp = 1514764800, convert_unix_to_date(UnixTimestamp, Date), writeln(Date).            
        

Output:
2023-08-23 23:43:34
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this example of Unix timestamp to date conversion using Prolog, stamp_date_time() is used to convert the Unix timestamp 1692834214 into a structured date (DateTime).
The format_time() predicate formats the date in the '%Y-%m-%d %H:%M:%S' format and returns a human-readable date string. The result is printed using writeln(Date), which in this exampple is 2023-08-23 23:43:34.


Other useful Prolog date functions

Function Description Example
stamp_date_time(+Stamp, -DateTime, +TimeZone) Converts a Unix timestamp to a structured date-time term. stamp_date_time(1514764800, DateTime, 'UTC') returns a date-time structure for 2018-01-01 00:00:00 UTC.
date_time_stamp(+DateTime, -Stamp) Converts a structured date-time term to a Unix timestamp. date_time_stamp(date(2018, 7, 15, 12, 30, 45, 0, 'UTC', -), Stamp) returns the Unix timestamp 1531656645.
date_time_value(+Component, +DateTime, -Value) Extracts the value of a specific component (like year, month, or day) from a structured date-time term. date_time_value(year, DateTime, Year) extracts the year component.
format_time(-Formatted, +Format, +DateTime) Formats a structured date-time term into a string using the given format. format_time(atom(Date), '%Y-%m-%d %H:%M:%S', DateTime) returns 2018-07-15 12:30:45.
get_time(-Stamp) Returns the current Unix timestamp. get_time(Stamp) returns the current Unix timestamp.
date(+Year, +Month, +Day, +Hour, +Minute, +Second, +Milli, +TimeZone, +DST) Represents a structured date-time term in Prolog. date(2018, 7, 15, 12, 30, 45, 0, 'UTC', -) represents the date 2018-07-15 12:30:45 UTC.
date_time_now(-DateTime) Returns the current date and time as a structured date-time term. date_time_now(DateTime) returns the current date and time.
date_time_value(+Component, +DateTime, -Value) Extracts components (year, month, etc.) from a date-time term. date_time_value(month, DateTime, Month) extracts the month from a date-time term.
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