Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in Prolog

🎈 🎈 🎈
+1

    :- use_module(library(date)).

    % Convert Date to Unix timestamp
    convert_date_to_unix(Year, Month, Day, Hour, Minute, Second, UnixTimestamp) :-
        date_time_stamp(date(Year, Month, Day, Hour, Minute, Second, 0, 'UTC', -), UnixTimestamp).
    
    % Example usage
    ?- convert_date_to_unix(2019, 6, 24, 18, 30, 22, UnixTimestamp), writeln(UnixTimestamp).            
        

Output:
1561401022
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Prolog example, the date_time_stamp() predicate converts the given date (2018-07-15 12:30:45) into a Unix timestamp.
The first argument is a date structure, and the second argument is the resulting Unix timestamp. The result is printed using writeln().



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