Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in Swift

🎈 🎈 🎈
+1

    import Foundation

    // Define value for unixTimestamp 
    let unixTimestamp: TimeInterval = 1323444771
    
    // Convert Unix timestamp to Date
    let date = Date(timeIntervalSince1970: unixTimestamp)
    
    // Format the date to a readable string
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" // Desired format: year-month-day hour:minute:second
    let formattedDate = dateFormatter.string(from: date)
    
    print("Converted Date: \(formattedDate)")            
        

Output:
Converted Date: 2011-12-09 15:32:51
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this snippet for converting a Unix timestamp to date in Swift, Date(timeIntervalSince1970: unixTimestamp) converts the Unix timestamp 1323444771 to a Date object. DateFormatter is used to specify the output format for the date. The format 'yyyy-MM-dd HH:mm:ss' represents the year, month, day, hour, minute, and second. The formatted date 2011-12-09 15:32:51 is then printed.


Other useful Swift date functions

Function Description Example
Date(timeIntervalSince1970:) Creates a Date object from a Unix timestamp (seconds since 1970-01-01). Date(timeIntervalSince1970: 1514764800) returns 2018-01-01 00:00:00
timeIntervalSince1970 Returns the number of seconds since 1970-01-01 for a given Date object. date.timeIntervalSince1970 returns 1531656645 (for July 15, 2018).
DateFormatter.dateFormat Sets the format for the DateFormatter to interpret or display a date. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" sets the format to year-month-day hour:minute:second.
DateFormatter.string(from:) Formats a Date object into a string using the specified format. dateFormatter.string(from: date) returns 2018-01-01 00:00:00
DateFormatter.date(from:) Parses a string into a Date object based on the specified format. dateFormatter.date(from: "2018-07-15 12:30:45") parses the string to create a Date object.
Date() Creates a Date object for the current date and time. Date() returns the current date and time.
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