2015-02-04 7 views
10

Nelle immagini qui sotto potete vedere il codice che ho scritto e i valori di tutte le variabili:Come faccio ad avere la corrente Data in formato breve Swift

class fun getCurrentShortDate() -> String { 
    var todaysDate = NSDate() 
    var dateFormatter = NSDateFormatter() 
    dateFormatter.dateFormat = "dd-MM-yyyy" 
    var DateInFormat = dateFormatter.stringFromDate(todaysDate) 

    return DateInFormat 
} 

Variable values

Come si può vedere la data corrente non è stata trovata, ma quando provo a cambiare NSDate in una stringa, semplicemente non lo farò.

+3

Non collegare al tuo codice: includilo nella tua domanda. –

+2

1. Non vergognarsi. Tutti iniziano da qualche parte. 2. Incollare il codice direttamente nel post piuttosto che pubblicare un'immagine – Aggressor

+2

Si può fare un println (DateInFormat) e dimostrare che la stringa è vuota? – Aggressor

risposta

7

È possibile creare un'estensione per trasformare facilmente un String in un 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)! 
    } 
} 

che nella vostra funzione è possibile NSDate().dateFromString("2015-02-04 23:29:28", format: "yyyy-MM-dd HH:mm:ss") e questo dovrebbe opere. Non è necessario che la data di input sia nello stesso formato di data di output.

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

L'uscita è 04-02-2015.

27

aggiornamento: Xcode 8.2 • Swift 3.0.2

extension DateFormatter { 
    convenience init(dateStyle: Style) { 
     self.init() 
     self.dateStyle = dateStyle 
    } 
    convenience init(timeStyle: Style) { 
     self.init() 
     self.timeStyle = timeStyle 
    } 
    convenience init(dateStyle: Style, timeStyle: Style) { 
     self.init() 
     self.dateStyle = dateStyle 
     self.timeStyle = timeStyle 
    } 
} 

extension Date { 
    static let shortDate = DateFormatter(dateStyle: .short) 
    static let fullDate = DateFormatter(dateStyle: .full) 

    static let shortTime = DateFormatter(timeStyle: .short) 
    static let fullTime = DateFormatter(timeStyle: .full) 

    static let shortDateTime = DateFormatter(dateStyle: .short, timeStyle: .short) 
    static let fullDateTime = DateFormatter(dateStyle: .full, timeStyle: .full) 

    var fullDate: String { return Date.fullDate.string(from: self) } 
    var shortDate: String { return Date.shortDate.string(from: self) } 

    var fullTime: String { return Date.fullTime.string(from: self) } 
    var shortTime: String { return Date.shortTime.string(from: self) } 

    var fullDateTime: String { return Date.fullDateTime.string(from: self) } 
    var shortDateTime: String { return Date.shortDateTime.string(from: self) } 
} 

Testing

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" 
+1

Funziona come un incantesimo! –

+0

può anche aggiungere var shortDateTime: String {return "\ (self.shortDate) \ (self.shortTime)"} – Booyah

7

Aggiornamento per Swift 3.0: creare un'estensione per 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: creare un'estensione per 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

Swift 3

Utilizzando l'estensione creata da @doovers e alcune stringhe di formato da questo website, si ottiene il seguente:

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 

Si potrebbe anche passare millisecondi a data oggetto come questo:

Date(1508577868947).string(format: "EEEE, MMM d, yyyy") // Saturday, Oct 21, 2017