Get NSDate from string using format

 Problem

I have a Date string and want to convert it to an NSDate object, using a specific date format.



 Recipe


// Initialize Date string
var dateStr = "2015-08-25"

// Set date format
var dateFmt = NSDateFormatter()
dateFmt.timeZone = NSTimeZone.defaultTimeZone()
dateFmt.dateFormat =  "yyyy-MM-dd"

// Get NSDate for the given string
var date = dateFmt.dateFromString(dateStr)

print(date!)
				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground