view raw
Ryan Ryan - 10 months ago 31
Swift Question

Getting Date and Time Swift

I have been looking into retrieving the date and time in Swift and this is the recommended strategy for getting a short, readable Date/Time output:

let currentDate = NSDate()
let formatter = NSDateFormatter()
formatter.locale = NSLocale.currentLocale()
formatter.dateStyle = .ShortStyle
formatter.timeStyle = .ShortStyle

let convertedDate = formatter.dateFromString(currentDate) //ERROR HERE


but this throws an Exception saying that
is not a valid parameter to be passed because it is of type
instead of

Can you help me understand why this is the case? I have only found similar approaches to this when retrieving Date and Time. Thank you very much, all help is appreciated!


You really want to go from a NSDate to a String, so use stringFromDate:

let convertedDate = formatter.stringFromDate(currentDate)