NSDateFormatter and Twitter

NSDateFormatter and Twitter

The NSDateFormatter class is super handy for scanning incoming text dates in order to create a NSDate object, so that you can then use for other date manipulation.

As handy as it is, it is not as well documented as one would hope.

As a bit of a teaser, I’ll list a version of a NSDateFormatter helper function that will take dates from the Twitter API and convert them into NSDate objects. I will flesh out the details behind the function in a post to come Thursday or Friday.

NSDateFormatter Helper:

-(NSDate*)dateFromTwitter:(NSString*)str {
static NSDateFormatter* sTwitter = nil;

if (str == nil) {
NSDate * today = [[[NSDate alloc] init] autorelease];
return today;
}

if (!sTwitter) {
sTwitter = [[NSDateFormatter alloc] init];
[sTwitter setTimeStyle:NSDateFormatterFullStyle];
[sTwitter setFormatterBehavior:NSDateFormatterBehavior10_4];
[sTwitter setDateFormat:@”EEE LLL dd HH:mm:ss Z yyyy”];
}
return [sTwitter dateFromString:str];
}

Advertisements

3 thoughts on “NSDateFormatter and Twitter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s