2

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.

lolo
  • 16,521
  • 9
  • 23
  • 49

1 Answers1

6

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];
Anoop Vaidya
  • 45,913
  • 15
  • 108
  • 138