Ryan Ryan - 1 year ago 65
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!

Answer Source

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

let convertedDate = formatter.stringFromDate(currentDate)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download