Swift:
Getting the current date
How to:
Swift’s Foundation framework provides the Date class, making it straightforward to get the current date and time. Here is a basic example of how to get the current date:
import Foundation
let currentDate = Date()
print(currentDate)This will output something like:
2023-04-12 07:46:23 +0000The output format follows the ISO 8601 standard, using the UTC time zone. However, you might want to format this date for display purposes. Swift’s DateFormatter class comes to the rescue:
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.timeStyle = .medium
let formattedDate = formatter.string(from: currentDate)
print(formattedDate)Sample output could be:
April 12, 2023 at 10:46:23 AMNote that the output format will vary depending on the locale of the device running the code.
For projects that require more complex date manipulation, many Swift developers turn to third-party libraries such as SwiftDate. Here’s how you might use SwiftDate to get the current date in a specific time zone and format:
First, add SwiftDate to your project using SPM, CocoaPods, or Carthage. Then:
import SwiftDate
let rome = Region(calendar: .gregorian, zone: .europeRome, locale: .current)
let currentDateInRome = DateInRegion(Date(), region: rome)
print(currentDateInRome.toFormat("yyyy-MM-dd HH:mm:ss"))This could output:
2023-04-12 09:46:23Using SwiftDate, you can easily manipulate dates and times for different time zones and locales, simplifying complex date handling tasks in your Swift applications.