iOS : Getting NSDate from NSString

Vinay Putta

Very useful, yet short and simple, code snippet on how to rip the NSDate from NSString using NSDateFormatter:

NSString *dateString = @”2014-01-19″;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

// this is imporant – we set our input date format to match our input string, if format doesn’t match you’ll get nil from your string, so be careful

[dateFormatter setDateFormat:@”yyyy-MM-dd”];

NSDate *dateFromString = [[NSDate alloc] init];

dateFromString = [dateFormatter dateFromString:dateString];

View original post