Date in Swift

Find this useful? Support us: Star on GitHub 6
Category: Other | Language: Swift

Sure, here's a brief explanation on how to use Date in Swift, along with some examples:

Date is a Swift structure that represents a point in time. It stores the number of seconds since January 1, 1970, at 00:00:00 UTC (Coordinated Universal Time), commonly called "Unix timestamp".

To use Date in Swift, you can create a new instance of the Date structure using the init() method or initializing it with a timestamp:

let currentDate = Date() // current date and time
let someTimestamp = TimeInterval(500000000) // a timestamp - here, 500000000 seconds since January 1, 1970
let someDate = Date(timeIntervalSince1970: someTimestamp) // creates a Date instance with the timestamp

You can also format a Date instance as a string using a DateFormatter. Here's an example of how to format the current date as a string:

let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy HH:mm:ss"
let dateString = formatter.string(from: currentDate)
print(dateString) // output: "02/10/2021 18:33:21"

You can manipulate dates using some of the built-in methods, like addingTimeInterval(_:) to add or subtract a number of seconds from a date, or compare(_:) to compare two dates:

let oneHourAgo = currentDate.addingTimeInterval(-3600) // subtracts 1 hour (3600 seconds) from the current date
if oneHourAgo.compare(currentDate) == .orderedAscending {
   print("The date one hour ago is earlier than the current date.")
}

Finally, it's worth mentioning that, by default, Date instances are represented in UTC time. If you need to work with dates in a specific time zone, you can use a TimeZone instance along with a Calendar instance. For example:

let newYorkTimeZone = TimeZone(identifier: "America/New_York")!
let calendar = Calendar.current

// get the current date in New York time zone
let newYorkDate = Date().addingTimeInterval(TimeInterval(newYorkTimeZone.secondsFromGMT()))
print(calendar.dateComponents([.year, .month, .day, .hour, .minute], from: newYorkDate, in: newYorkTimeZone))
// outputs: year: 2021, month: 10, day: 2, hour: 14, minute: 05

I hope this helps!