Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in SML (Standard ML)

How to convert Datetime to Unix timestamp in SML (Standard ML)

🎈 🎈 🎈
+1

    (* Convert Date to Unix timestamp in SML *)
    val date = Date.fromTime (2018, Date.Jan, 1, 0, 0, 0);  (* 2018-01-01 00:00:00 *)
    val unix_timestamp = Time.toSeconds (Date.toTime date);
    
    (* Print the Unix timestamp *)
    val _ = print ("Unix Timestamp: " ^ Int.toString unix_timestamp ^ "\n");            
        

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

Code Explanation

In this SML date to Unix timestamp conversion snippet Date.fromTime (2018, Date.Jan, 1, 0, 0, 0) creates a Date.date structure representing '2018-01-01 00:00:00'. Date.toTime date converts the Date.date structure into a Time.time value. Time.toSeconds (Date.toTime date) converts the Time.time value to the Unix timestamp, which is printed using print.



Other useful SML (Standard ML) date functions

Function Description Example
Time.fromSeconds() Converts an integer (Unix timestamp in seconds) to a Time.time value. Time.fromSeconds 1514764800 returns a Time.time value representing 2018-01-01 00:00:00
Time.toDate() Converts a Time.time value to a Date.date structure. Time.toDate (Time.fromSeconds 1514764800) returns the date 2018-01-01 00:00:00
Time.toSeconds() Converts a Time.time value to a Unix timestamp (in seconds). Time.toSeconds (Date.toTime date) returns the Unix timestamp 1514764800
Date.fromTime() Creates a Date.date structure from year, month, day, hour, minute, and second. Date.fromTime (2018, Date.Jan, 1, 0, 0, 0) returns 2018-01-01 00:00:00
Date.toTime() Converts a Date.date structure to a Time.time value. Date.toTime (Date.fromTime (2018, Date.Jan, 1, 0, 0, 0)) returns a Time.time value for 2018-01-01 00:00:00
Date.year() Extracts the year from a Date.date structure. Date.year (Date.fromTime (2018, Date.Jan, 1, 0, 0, 0)) returns 2018
Date.month() Extracts the month from a Date.date structure. Date.month (Date.fromTime (2018, Date.Jan, 1, 0, 0, 0)) returns Date.Jan
Date.day() Extracts the day of the month from a Date.date structure. Date.day (Date.fromTime (2018, Date.Jan, 1, 0, 0, 0)) returns 1
Date.hour() Extracts the hour from a Date.date structure. Date.hour (Date.fromTime (2018, Date.Jan, 1, 0, 0, 0)) returns 0
Date.minute() Extracts the minute from a Date.date structure. Date.minute (Date.fromTime (2018, Date.Jan, 1, 0, 0, 0)) returns 0
Date.second() Extracts the second from a Date.date structure. Date.second (Date.fromTime (2018, Date.Jan, 1, 0, 0, 0)) returns 0
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