Reputation: 7900
I have a string that contain date:
2012-05-25
and i wan to convert this string to NSDate:
NSString *birthday = /*2012-05-25*/;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-DD"];
NSDate *dateFromString = [dateFormatter dateFromString:birthday];
and in the dateFromString i get the date of today.
Upvotes: 0
Views: 284
Reputation: 2312
I hope this helps u. try this
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(@"%@", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}
Upvotes: 0
Reputation:
This works :
NSString *birthday = @"2012-05-25";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *dateFromString = [dateFormatter dateFromString:birthday];
NSLog(@"%@", dateFromString);
Outputs :
2012-01-25 11:36:38.157 dsv[2679:903] 2012-05-25 00:00:00 +0200
Please refer to the Apple guide for details.
Upvotes: 2