Friday, May 10, 2013

NSDateFormatter dateFromString example ios


Returns a date representation of a given string interpreted using the receiver’s current settings.
- (NSDate *)dateFromString:(NSString *)string
The string to parse.
Return Value
A date representation of string interpreted using the receiver’s current settings.
Example of [NSDateFormatter dateFromString]
NSString *test = @"2009-10-16T09:42:24.999605";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *testOut = [dateFormatter dateFromString:test];
Example of [NSDateFormatter dateFromString]
NSString * dateString = @"2010-05-24 at 20:45" // actually downloaded from the internet
NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd 'at' hh:mm"];
NSDate * dateFromString = [myDateFormatter dateFromString:dateString];
Example of [NSDateFormatter dateFromString]
// dateString comes from my data source, and look like I've said like this.
dateString = @"2011-04-11 23:12:05";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:dateString];