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
print("\n\(convertedDate)")
but this throws an Exception saying that currentDate
is not a valid parameter to be passed because it is of type NSDate
instead of String
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)