Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

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

🎈 🎈 🎈
+1

    (* Convert Unix timestamp to Date in SML *)
    val unix_timestamp = Time.fromSeconds 1721818631;
    val date = Time.toDate unix_timestamp;
    
    (* Extract the date components *)
    val year = Date.year date;
    val month = Date.month date;
    val day = Date.day date;
    val hour = Date.hour date;
    val minute = Date.minute date;
    val second = Date.second date;
    
    (* Print the result *)
    val _ = print ("Converted Date: " ^ Int.toString year ^ "-" ^
                    Int.toString (Date.monthToInt month) ^ "-" ^
                    Int.toString day ^ " " ^
                    Int.toString hour ^ ":" ^
                    Int.toString minute ^ ":" ^
                    Int.toString second ^ "\n");            
        

Output:
Converted Date: 2024-07-24 10:57:11
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this SML snippet Time.fromSeconds 1721818631 converts the Unix timestamp 1721818631 into an SML Time.time value. Time.toDate unix_timestamp converts this Time.time value into a Date.date structure. The components (year, month, day, etc.) are extracted using functions like Date.year, Date.month, Date.day, etc.
The month is converted to an integer using Date.monthToInt, and the result, 2024-07-24 10:57:11, 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