开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜