Date and Time Handling in Swift

Learn how to work with dates and times in Swift, from retrieving the current date to formatting it as a string.

Date and Time Handling in Swift
Photo by Rubén Menárguez / Unsplash

Learn how to work with dates and times in Swift, from retrieving the current date to formatting it as a string.

// Get the current date and time
let currentDate = Date()
print(currentDate)

// Format a date as a string
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatter.string(from: currentDate)
print(dateString)