Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in F#

🎈 🎈 🎈
+1

    open System

    // Convert Unix timestamp to DateTime
    let unixTimestamp = 913875349L
    let dateTime = DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).DateTime
    
    printfn "Converted Date: %A" dateTime            
        

Output:
Converted Date: 1998-12-17 06:15:49
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this F# example, the Unix timestamp '913875349' is converted to a DateTime object using the DateTimeOffset.FromUnixTimeSeconds() method.
The result is the date '1998-12-17 06:15:49'. The .DateTime property extracts the DateTime value from the DateTimeOffset object, and printfn is used to display the converted date in the console.



Other useful F# date functions

Function Description Example
DateTime.Now Returns the current local date and time as a DateTime object. DateTime.Now returns 2024-08-28 12:34:56
DateTime.UtcNow Returns the current UTC date and time as a DateTime object. DateTime.UtcNow returns 2024-08-28 16:34:56
DateTimeOffset.FromUnixTimeSeconds() Converts a Unix timestamp (in seconds) to a DateTimeOffset object. DateTimeOffset.FromUnixTimeSeconds(1652602245L) returns 2022-05-15 08:10:45 UTC
DateTimeOffset.ToUnixTimeSeconds() Converts a DateTimeOffset object to a Unix timestamp in seconds. dateTimeOffset.ToUnixTimeSeconds() returns 1652602245
DateTime.Add() Adds a specified time interval to a DateTime object. DateTime.Now.Add(TimeSpan.FromDays(5.0)) adds 5 days to the current date.
DateTime.Subtract() Subtracts a specified time interval from a DateTime object. DateTime.Now.Subtract(TimeSpan.FromHours(3.0)) subtracts 3 hours from the current time.
DateTime.Parse() Parses a string representation of a date and time into a DateTime object. DateTime.Parse("2024-08-28T12:34:56") returns 2024-08-28 12:34:56
TimeSpan.FromDays() Creates a TimeSpan object representing a specified number of days. TimeSpan.FromDays(7.0) returns a time span of 7 days.
DateTime.ToString() Formats a DateTime object as a string. dateTime.ToString("yyyy-MM-dd HH:mm:ss") returns 2024-08-28 12:34:56
DateTime.Compare() Compares two DateTime objects and returns an integer indicating if the first is earlier, the same, or later than the second. DateTime.Compare(date1, date2) returns -1, 0, or 1
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