In the images below you can see the code I wrote and the values of all the variables:
class fun getCurrentShortDate() -> String {
var todaysDate = NSD
Swift 3
Using the extension created by @doovers and some format strings from this website, you get the following:
extension Date {
func string(format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
}
Usage:
Date().string(format: "EEEE, MMM d, yyyy") // Saturday, Oct 21, 2017
Date().string(format: "MM/dd/yyyy") // 10/21/2017
Date().string(format: "MM-dd-yyyy HH:mm") // 10-21-2017 03:31
Date().string(format: "MMM d, h:mm a") // Oct 21, 3:31 AM
Date().string(format: "MMMM yyyy") // October 2017
Date().string(format: "MMM d, yyyy") // Oct 21, 2017
Date().string(format: "E, d MMM yyyy HH:mm:ss Z") // Sat, 21 Oct 2017 03:31:40 +0000
Date().string(format: "yyyy-MM-dd'T'HH:mm:ssZ") // 2017-10-21T03:31:40+0000
Date().string(format: "dd.MM.yy") // 21.10.17
You could also pass milliseconds to date object like this:
Date(1508577868947).string(format: "EEEE, MMM d, yyyy") // Saturday, Oct 21, 2017