Epoch and Date Time Conversion in Swift


📅 Last updated:


Date and time handling is an important part of application development. Swift provides powerful date and time functionality through the Foundation framework. With Swift, we can work with Date objects, get the current Unix timestamp, convert Unix timestamps to dates, and convert dates back to Unix or epoch timestamps.

In this guide, we will explain Swift date and time functionality and show how to get the current epoch or Unix timestamp, convert timestamps to human-readable dates, convert dates to Unix timestamps, and format dates and times.


Get Current Date and Time in Swift

The Swift Date() initializer creates a Date object representing the current date and time.

import Foundation

let date = Date()

print(date)

A Date represents a specific point in time. When you print a Date directly, Swift displays a textual representation of that point in time.


Get Epoch or Unix Timestamp in Swift

Swift provides the timeIntervalSince1970 property to get the number of seconds between a Date and the Unix epoch, which starts at January 1, 1970 UTC.

import Foundation

let date = Date()
let timestamp = date.timeIntervalSince1970

print(timestamp)

The result is a floating-point value and can contain fractional seconds.

Get Unix Timestamp in Seconds

If you need a Unix timestamp as a whole number of seconds, convert the value to an Int.

import Foundation

let timestamp = Int(Date().timeIntervalSince1970)

print(timestamp)

Example output:

178XXXXXXX

Get Unix Timestamp in Milliseconds in Swift

Some APIs and JavaScript applications use Unix timestamps in milliseconds instead of seconds. You can multiply timeIntervalSince1970 by 1000.

import Foundation

let timestampMilliseconds =
    Int(Date().timeIntervalSince1970 * 1000)

print(timestampMilliseconds)

Remember that Swift's timeIntervalSince1970 value is expressed in seconds. Multiply by 1000 when you need milliseconds.


Convert Epoch or Unix Timestamp to Date in Swift

Swift provides the Date(timeIntervalSince1970:) initializer to convert a Unix timestamp into a Date.

import Foundation

let unixTimestamp: TimeInterval = 1767225600

let date = Date(
    timeIntervalSince1970: unixTimestamp
)

print(date)

The timestamp is interpreted as seconds from the Unix epoch.

Convert Millisecond Timestamp to Date

If the timestamp is in milliseconds, divide it by 1000 before passing it to Date(timeIntervalSince1970:).

import Foundation

let timestampMilliseconds: TimeInterval = 1767225600000

let date = Date(
    timeIntervalSince1970: timestampMilliseconds / 1000
)

print(date)

Convert Date to Epoch or Unix Timestamp in Swift

You can convert any Swift Date into a Unix timestamp using timeIntervalSince1970.

import Foundation

let date = Date()

let unixTimestamp = date.timeIntervalSince1970

print(unixTimestamp)

To get a whole-number timestamp:

let unixTimestamp = Int(date.timeIntervalSince1970)

print(unixTimestamp)

Convert a Specific Date to Unix Timestamp in Swift

You can use Calendar and DateComponents when you need to create a specific date and time.

import Foundation

var calendar = Calendar(identifier: .gregorian)

calendar.timeZone = TimeZone(secondsFromGMT: 0)!

var components = DateComponents()

components.year = 2026
components.month = 1
components.day = 1
components.hour = 0
components.minute = 0
components.second = 0

if let date = calendar.date(from: components) {

    let timestamp = Int(
        date.timeIntervalSince1970
    )

    print(timestamp)
}

Format Date and Time in Swift

Swift's Foundation framework provides DateFormatter for converting Date values into human-readable date and time strings.

import Foundation

let date = Date()

let formatter = DateFormatter()

formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

let formattedDate = formatter.string(
    from: date
)

print(formattedDate)

Example output:

2026-09-27 15:30:00

Convert Unix Timestamp to Human Readable Date in Swift

We can combine Date(timeIntervalSince1970:) and DateFormatter to convert a Unix timestamp into a human-readable date.

import Foundation

let unixTimestamp: TimeInterval = 1767225600

let date = Date(
    timeIntervalSince1970: unixTimestamp
)

let formatter = DateFormatter()

formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

formatter.timeZone = TimeZone(secondsFromGMT: 0)

let formattedDate = formatter.string(
    from: date
)

print(formattedDate)

Output:

2026-01-01 00:00:00

Get Current Day, Month and Year in Swift

Swift's Calendar can be used to extract individual components such as the day, month and year from a Date.

import Foundation

let date = Date()

let calendar = Calendar.current

let day = calendar.component(
    .day,
    from: date
)

let month = calendar.component(
    .month,
    from: date
)

let year = calendar.component(
    .year,
    from: date
)

print("Day: \(day)")
print("Month: \(month)")
print("Year: \(year)")

Get Current Date in Day-Month-Year Format

You can use DateFormatter when you need a specific date format.

import Foundation

let date = Date()

let formatter = DateFormatter()

formatter.dateFormat = "dd-MM-yyyy"

let fullDate = formatter.string(
    from: date
)

print(fullDate)

Example output:

27-09-2026

Convert Unix Timestamp to ISO 8601 Date in Swift

Swift Foundation includes ISO8601DateFormatter for working with ISO 8601 date and time strings.

import Foundation

let timestamp: TimeInterval = 1767225600

let date = Date(
    timeIntervalSince1970: timestamp
)

let formatter = ISO8601DateFormatter()

let isoDate = formatter.string(
    from: date
)

print(isoDate)

Output:

2026-01-01T00:00:00Z

Convert ISO 8601 Date to Unix Timestamp in Swift

You can also convert an ISO 8601 date string to a Date and then obtain its Unix timestamp.

import Foundation

let isoString = "2026-01-01T00:00:00Z"

let formatter = ISO8601DateFormatter()

if let date = formatter.date(
    from: isoString
) {

    let timestamp = Int(
        date.timeIntervalSince1970
    )

    print(timestamp)
}

Swift Unix Timestamp Conversion Examples

Conversion Swift Code
Current Unix timestamp Int(Date().timeIntervalSince1970)
Current timestamp with milliseconds Int(Date().timeIntervalSince1970 * 1000)
Timestamp to Date Date(timeIntervalSince1970: timestamp)
Date to timestamp date.timeIntervalSince1970
Date formatting DateFormatter()
ISO 8601 formatting ISO8601DateFormatter()

Complete Swift Epoch Conversion Example

The following example demonstrates the most common Unix timestamp operations in one Swift program.

import Foundation

// Current date and time
let now = Date()

// Current Unix timestamp
let timestamp = now.timeIntervalSince1970

// Unix timestamp in seconds
let timestampSeconds = Int(timestamp)

// Unix timestamp in milliseconds
let timestampMilliseconds = Int(timestamp * 1000)

// Convert timestamp back to Date
let date = Date(
    timeIntervalSince1970: timestamp
)

// Format date
let formatter = DateFormatter()

formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

let formattedDate = formatter.string(
    from: date
)

// ISO 8601
let isoFormatter = ISO8601DateFormatter()

let isoDate = isoFormatter.string(
    from: date
)

print("Date: \(date)")
print("Unix Timestamp: \(timestampSeconds)")
print("Milliseconds: \(timestampMilliseconds)")
print("Formatted Date: \(formattedDate)")
print("ISO 8601: \(isoDate)")

Swift Date and Unix Timestamp Quick Reference

Use these Swift Foundation APIs for the most common date and timestamp conversion tasks.

// Current date
let date = Date()

// Unix timestamp in seconds
let timestamp = date.timeIntervalSince1970

// Unix timestamp as integer
let seconds = Int(date.timeIntervalSince1970)

// Unix timestamp in milliseconds
let milliseconds =
    Int(date.timeIntervalSince1970 * 1000)

// Unix timestamp to Date
let convertedDate =
    Date(timeIntervalSince1970: timestamp)

// Format Date
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

let formatted =
    formatter.string(from: date)

// ISO 8601
let isoFormatter = ISO8601DateFormatter()

let isoString =
    isoFormatter.string(from: date)

You can use our Unix Timestamp Converter to convert Unix timestamps and human-readable dates online.