copenndthagen
copenndthagen

Reputation: 50740

iphone parse year from 04-30-2006

How do I parse the year value from 04-30-2006 as string?

Upvotes: 0

Views: 78

Answers (3)

Dave DeLong
Dave DeLong

Reputation: 243156

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];

Upvotes: 2

Aditya
Aditya

Reputation: 4396

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

Upvotes: 0

TomSwift
TomSwift

Reputation: 39502

NSString* year = [[myDateString componentsSeparatedByString: @"-"] objectAtIndex: 2];

Upvotes: 1

Related Questions