To convert a Unix timestamp to a date in Swift, you can use the Date(timeIntervalSince1970: TimeInterval)
initializer on the Date
struct.
Here's an example implementation:
main.swift310 chars8 lines
This function takes in a Unix timestamp (in seconds), creates a new Date
object using the timeIntervalSince1970
initializer, and then formats the date into the desired format using a DateFormatter
object.
To use this function, you can simply call it and pass in your Unix timestamp as a Double
value:
main.swift83 chars3 lines
This would output 2020-05-04 12:44:20
, which is the formatted date string for the Unix timestamp 1588583460
.
gistlibby LogSnag