How do I get the current Date in short format in Swift
Solution 1
Xcode 11 or later • Swift 5.1 or later
extension TimeZone {
static let gmt = TimeZone(secondsFromGMT: 0)!
}
extension Locale {
static let ptBR = Locale(identifier: "pt_BR")
}
extension Formatter {
static let date = DateFormatter()
}
extension Date {
func localizedDescription(date dateStyle: DateFormatter.Style = .medium,
time timeStyle: DateFormatter.Style = .medium,
in timeZone: TimeZone = .current,
locale: Locale = .current,
using calendar: Calendar = .current) -> String {
Formatter.date.calendar = calendar
Formatter.date.locale = locale
Formatter.date.timeZone = timeZone
Formatter.date.dateStyle = dateStyle
Formatter.date.timeStyle = timeStyle
return Formatter.date.string(from: self)
}
var localizedDescription: String { localizedDescription() }
}
Date().localizedDescription // "18 Dec 2021 06:43:50"
Date().localizedDescription(in: .gmt) // "18 Dec 2021 06:43:50" UTC time
Date().localizedDescription(date: .short, time: .short) // "18/12/21 03:43"
Date().localizedDescription(date: .full, time: .full) // "Saturday, 18 December 2021 03:43:50 Brasilia Standard Time"
Date().localizedDescription(date: .full, time: .full, in: .gmt) // "Saturday, 18 December 2021 06:43:50 Greenwich Mean Time"
Date().localizedDescription(date: .full, time: .full, locale: .ptBR) // "sábado, 18 de dezembro de 2021 03:43:50 Horário Padrão de Brasília"
extension Date {
var fullDate: String { localizedDescription(date: .full, time: .none) }
var longDate: String { localizedDescription(date: .long, time: .none) }
var mediumDate: String { localizedDescription(date: .medium, time: .none) }
var shortDate: String { localizedDescription(date: .short, time: .none) }
var fullTime: String { localizedDescription(date: .none, time: .full) }
var longTime: String { localizedDescription(date: .none, time: .long) }
var mediumTime: String { localizedDescription(date: .none, time: .medium) }
var shortTime: String { localizedDescription(date: .none, time: .short) }
var fullDateTime: String { localizedDescription(date: .full, time: .full) }
var longDateTime: String { localizedDescription(date: .long, time: .long) }
var mediumDateTime: String { localizedDescription(date: .medium, time: .medium) }
var shortDateTime: String { localizedDescription(date: .short, time: .short) }
}
print(Date().fullDate) // "Saturday, 18 December 2021\n"
print(Date().shortDate) // "18/12/21\n"
print(Date().fullTime) // "03:43:50 Brasilia Standard Time\n"
print(Date().shortTime) // "03:43\n"
print(Date().fullDateTime) // "Saturday, 18 December 2021 03:43:50 Brasilia Standard Time\n"
print(Date().shortDateTime) // "18/12/21 03:43\n"
Solution 2
SWIFT 4 or 5
extension Date {
static func getCurrentDate() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy HH:mm:ss"
return dateFormatter.string(from: Date())
}
}
Using
print(Date.getCurrentDate())
Solution 3
Update for Swift 3.0:
Create an extension for Date
extension Date {
func string(format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
}
Usage:
Date().string(format: "yyyy-MM-dd")
Swift 2.2:
Create an extension for NSDate
extension NSDate {
func dateStringWithFormat(format: String) -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.stringFromDate(self)
}
}
Usage:
NSDate().dateStringWithFormat("yyyy-MM-dd")
Solution 4
You can create a extension for easily transform a String
into a NSDate
.
extension NSDate {
func dateFromString(date: String, format: String) -> NSDate {
let formatter = NSDateFormatter()
let locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.locale = locale
formatter.dateFormat = format
return formatter.dateFromString(date)!
}
}
Than in your function you can NSDate().dateFromString("2015-02-04 23:29:28", format: "yyyy-MM-dd HH:mm:ss")
and this should works. Input date don't need to be on the same format of output date.
func getCurrentShortDate() -> String {
var todaysDate = NSDate().dateFromString("2015-02-04 23:29:28", format: "yyyy-MM-dd HH:mm:ss")
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
var DateInFormat = dateFormatter.stringFromDate(todaysDate)
return DateInFormat
}
println(getCurrentShortDate())
The output is 04-02-2015
.
Solution 5
Swift 3
Using the extension created by @doovers and some format strings from this website, you get the following:
extension Date {
func string(format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
}
Usage:
Date().string(format: "EEEE, MMM d, yyyy") // Saturday, Oct 21, 2017
Date().string(format: "MM/dd/yyyy") // 10/21/2017
Date().string(format: "MM-dd-yyyy HH:mm") // 10-21-2017 03:31
Date().string(format: "MMM d, h:mm a") // Oct 21, 3:31 AM
Date().string(format: "MMMM yyyy") // October 2017
Date().string(format: "MMM d, yyyy") // Oct 21, 2017
Date().string(format: "E, d MMM yyyy HH:mm:ss Z") // Sat, 21 Oct 2017 03:31:40 +0000
Date().string(format: "yyyy-MM-dd'T'HH:mm:ssZ") // 2017-10-21T03:31:40+0000
Date().string(format: "dd.MM.yy") // 21.10.17
You could also pass milliseconds to date object like this:
Date(1508577868947).string(format: "EEEE, MMM d, yyyy") // Saturday, Oct 21, 2017
Admin
Updated on December 24, 2021Comments
-
Admin over 2 years
In the images below you can see the code I wrote and the values of all the variables:
class fun getCurrentShortDate() -> String { var todaysDate = NSDate() var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" var DateInFormat = dateFormatter.stringFromDate(todaysDate) return DateInFormat }
As you can see the current date is found no problem, but when I try to change the NSDate to a string, it just won't do it.