Instead, the date format should be assigned to the dateFormat property of the date formatter.
let date = NSDate.date() let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" let str = dateFormatter.stringFromDate(date) println(str)
This prints the date using the default time zone on the device. only if you want the output according to a different time zone, add for example
Swift 3. *
dateFormatter.timeZone = NSTimeZone(name: "UTC")
Swift 4. *
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
also link to the link http://www.brianjcoleman.com/tutorial-nsdate-in-swift/
source share