How can I get my current date ios Swift?

10.8k views Asked by At

Specifically I would like to print my current date formatted like this - 2016-12-08 03:37:22 +0000. I've tried NSDateComponents. Swift 2.1 is telling me NSSecondCalendarUnit is deprecated and it doesn't recognize NSCalendarUnitSecond. What is the best way to do this?

1

There are 1 answers

1
AhiyaHiya On

I put this code together just by reading the iOS Swift DateFormatter documentation available in Xcode:

let formatter = DateFormatter()
//2016-12-08 03:37:22 +0000
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let now = Date()
let dateString = formatter.string(from:now)
NSLog("%@", dateString)