iphone parse year from 04-30-2006
How do I parse the year value from 04-30-200开发者_开发知识库6 as string?
The proper way to do this, as @Aditya alludes, is to use an NSDateFormatter
:
NSDateFormatter * f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"MM-dd-yyyy"];
NSDate * date = [f dateFromString:@"04-30-2006"];
[f release];
NSString* year = [[myDateString componentsSeparatedByString: @"-"] objectAtIndex: 2];
You can use this way
NSString *str1=@"Your date";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[NSTimeZone resetSystemTimeZone];
NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:gmtZone];
[dateFormatter setDateFormat:@"ddd MMM yyyy HH:mm:ss Z"];
NSDate *dateT = [dateFormatter dateFromString:str1];
Cheers
精彩评论