views:

26

answers:

1

I need to get the current date.

Then add a year to it.

And output the result in the format YYYY-MM-DD aka 2011-11-20

+1  A: 

You will want to make use of NSCalendar and NSDateComponents in order to perform what you're after. Something like the following should do the trick.

NSDate *todaysDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:1];
NSDate *targetDate = [gregorian dateByAddingComponents:dateComponents toDate:todaysDate  options:0];
[dateComponents release];
[gregorian release];

To then output the target date as a string in the format specified, you can do the following;

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; 
NSString* dateString = [dateFormatter stringFromDate:targetDate];

Hope this helps.

John Wordsworth
How do I output that as a string ?
Jules
Sorry Jules, missed that bit off the end. Have updated the original answer to include code that shows how to then convert the date to the desired string format.
John Wordsworth