Swift – Get Current Date
In this tutorial, we will learn how to get the current date in Swift. We will explore the Date
class, use date formatting, and retrieve the current date and time in different formats with practical examples.
Getting the Current Date in Swift
In Swift, the Date
class represents a specific point in time. To get the current date and time, you can use Date()
, which creates a new instance representing the current date and time.
For example:
let currentDate = Date()
print("Current Date and Time: \(currentDate)")
This prints the current date and time in the default format.
Formatting the Date
To format the date in a human-readable style, you can use the DateFormatter
class. The DateFormatter
allows you to specify how the date and time should be displayed, such as short, medium, or custom formats.
Here’s an example to display the current date in a custom format:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let formattedDate = dateFormatter.string(from: currentDate)
print("Formatted Date and Time: \(formattedDate)")
Explanation:
dateFormatter.dateFormat
: Specifies the custom date format. For example:yyyy
: YearMM
: Monthdd
: DayHH
: Hour (24-hour clock)mm
: Minutess
: Second
dateFormatter.string(from: currentDate)
: Converts theDate
object into a formatted string.
Displaying the Date in Predefined Styles
The DateFormatter
class provides predefined styles for displaying the date and time. You can use properties such as .short
, .medium
, .long
, and .full
to format the date.
Here’s an example:
let shortFormatter = DateFormatter()
shortFormatter.dateStyle = .short
shortFormatter.timeStyle = .short
print("Short Style: \(shortFormatter.string(from: currentDate))")
let longFormatter = DateFormatter()
longFormatter.dateStyle = .long
longFormatter.timeStyle = .long
print("Long Style: \(longFormatter.string(from: currentDate))")
Explanation:
.short
: Displays a concise version of the date and time..long
: Displays a detailed version of the date and time..dateStyle
: Specifies how the date should be formatted..timeStyle
: Specifies how the time should be formatted.
Complete Swift Program
Here’s the complete Swift program to demonstrate getting and formatting the current date:
import Foundation
// Get the current date
let currentDate = Date()
print("Current Date and Time: \(currentDate)")
// Format the date using a custom format
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let formattedDate = dateFormatter.string(from: currentDate)
print("Formatted Date and Time: \(formattedDate)")
// Display date in predefined styles
let shortFormatter = DateFormatter()
shortFormatter.dateStyle = .short
shortFormatter.timeStyle = .short
print("Short Style: \(shortFormatter.string(from: currentDate))")
let longFormatter = DateFormatter()
longFormatter.dateStyle = .long
longFormatter.timeStyle = .long
print("Long Style: \(longFormatter.string(from: currentDate))")
Output:
Current Date and Time: 2024-11-19 14:53:29 +0000
Formatted Date and Time: 2024-11-19 14:53:29
Short Style: 11/19/24, 2:53 PM
Long Style: November 19, 2024 at 2:53:29 PM UTC
Xcode Screenshot: