How do I get the current Date in short format in Swift How do I get the current Date in short format in Swift swift swift

How do I get the current Date in short format in Swift


Xcode 11 or later • Swift 5.1 or later


extension TimeZone {    static let gmt = TimeZone(secondsFromGMT: 0)!}extension Formatter {    static let date = DateFormatter()}

extension Date {    func localizedDescription(dateStyle: DateFormatter.Style = .medium,                              timeStyle: DateFormatter.Style = .medium,                           in timeZone : TimeZone = .current,                              locale   : Locale = .current) -> String {        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                                                // "Sep 26, 2018 at 12:03:41 PM"Date().localizedDescription(in: .gmt)                                      // "Sep 26, 2018 at 3:03:41 PM" UTC TIMEDate().localizedDescription(dateStyle: .short, timeStyle: .short)          //  "9/26/18, 12:03 PM"Date().localizedDescription(dateStyle: .full, timeStyle: .full)            //  "Wednesday, September 26, 2018 at 12:03:41 PM Brasilia Standard Time"Date().localizedDescription(dateStyle: .full, timeStyle: .full, in: .gmt)  // "Wednesday, September 26, 2018 at 3:03:41 PM Greenwich Mean Time"

extension Date {    var fullDate: String   { localizedDescription(dateStyle: .full,   timeStyle: .none) }    var longDate: String   { localizedDescription(dateStyle: .long,   timeStyle: .none) }    var mediumDate: String { localizedDescription(dateStyle: .medium, timeStyle: .none) }    var shortDate: String  { localizedDescription(dateStyle: .short,  timeStyle: .none) }    var fullTime: String   { localizedDescription(dateStyle: .none,   timeStyle: .full) }    var longTime: String   { localizedDescription(dateStyle: .none,   timeStyle: .long) }    var mediumTime: String { localizedDescription(dateStyle: .none,   timeStyle: .medium) }    var shortTime: String  { localizedDescription(dateStyle: .none,   timeStyle: .short) }    var fullDateTime: String   { localizedDescription(dateStyle: .full,   timeStyle: .full) }    var longDateTime: String   { localizedDescription(dateStyle: .long,   timeStyle: .long) }    var mediumDateTime: String { localizedDescription(dateStyle: .medium, timeStyle: .medium) }    var shortDateTime: String  { localizedDescription(dateStyle: .short,  timeStyle: .short) }}

print(Date().fullDate)  // "Friday, May 26, 2017\n"print(Date().shortDate)  // "5/26/17\n"print(Date().fullTime)  // "10:16:24 AM Brasilia Standard Time\n"print(Date().shortTime)  // "10:16 AM\n"print(Date().fullDateTime)  // "Friday, May 26, 2017 at 10:16:24 AM Brasilia Standard Time\n"print(Date().shortDateTime)  // "5/26/17, 10:16 AM\n"


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")


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())