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

前端 未结 7 805
北荒
北荒 2020-11-22 15:10

In the images below you can see the code I wrote and the values of all the variables:

class fun getCurrentShortDate() -> String {
    var todaysDate = NSD         


        
相关标签:
7条回答
  • 2020-11-22 15:48

    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")
    
    0 讨论(0)
  • 2020-11-22 15:50

    Here's the way Apple suggests

        let formatter = DateFormatter()
        formatter.locale = Locale.current
        formatter.dateStyle = .short
        let dateString = formatter.string(from: date)
    

    or if the pre-defined format of .short, .medium, .long and .full are quite right create the template in a locale

        let formatter = DateFormatter()
        formatter.locale = Locale.current
        formatter.setLocalizedDateFormatFromTemplate("MM/dd/yyyy")
        let dateString = formatter.string(from: date)
    

    Here's the link

    0 讨论(0)
  • 2020-11-22 15:52

    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.

    0 讨论(0)
  • 2020-11-22 15:57
     let dateformatter1 = DateFormatter()
        dateformatter1.dateFormat = "ccc, d MMM yyy"
    
        let dateString1 = dateformatter1.string(from: datePicker.date)
        print("Date Selected \(dateString1)")
        labelDate.text = dateString1
    
        let dateformatter2 = DateFormatter()
        dateformatter2.dateFormat = "dd-MM-yyyy"
        let dateString2 = dateformatter2.string(from: datePicker.date)
        print("Date Selected \(dateString2)")
    
    
        let dateformatter3 = DateFormatter()
        dateformatter3.dateFormat = "dd/MM/yyyy"
        let dateString3 = dateformatter3.string(from: datePicker.date)
        print("Date Selected \(dateString3)")
    
        let dateformatter4 = DateFormatter()
        dateformatter4.dateFormat = "dd MMMM yyyy hh:mm a"
        let dateString4 = dateformatter4.string(from: datePicker.date)
        print("Date Selected \(dateString4)") 
    

    I referred this article on Codzify.com. Really helpful.

    0 讨论(0)
  • 2020-11-22 15:58

    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
    
    0 讨论(0)
  • 2020-11-22 16:11

    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 TIME
    Date().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"
    
    0 讨论(0)
提交回复
热议问题