lolo
lolo

Reputation: 17894

Get specific date - Objective c iphone

I want to get the date that will be in 45 days from today in objective C - iphone. I'm new in objective C :)

I know that i can do somthing like:

NSString *dateStr = @"Tue, 16 April 2013 13:00:00 +0000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr]; 
[dateFormat release];

But, i do not want a static allocate... it should be dynamic. Every day i need to get in *date variable the date of 45 days from today.

Upvotes: 2

Views: 163

Answers (1)

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

Try this way:

NSDate *today=[NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components=[[NSDateComponents alloc] init];
components.day=45;
NSDate *targetDate =[calendar dateByAddingComponents:components toDate:today options: 0];

Upvotes: 6

Related Questions