How to get the next 15-day date depending on the current date of the day?

Currently I can get the current iPhone date using the following code

NSDate* date = [NSDate date]; NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSString* currentDateStr = [formatter stringFromDate:date]; NSLog(@"User current Date:%@",currentDateStr); 

but I want to get the next 15 days from the current date, how can I get this?

+4
source share
5 answers

Try the following:

 NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease]; NSDateComponents* components = [[[NSDateComponents alloc] init] autorelease]; components.day = 15; NSDate* newDate = [calendar dateByAddingComponents: components toDate: self.date options: 0]; 
+6
source
 NSDate *todayDate = [NSDate date]; NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setDay:+15]; NSDate *afterfifteenDays = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:todayDate options:0]; NSLog(@"todayDate: %@", todayDate); NSLog(@"afterfifteenDays: %@", afterfifteenDays); 
+1
source

Try the following:

 NSTimeInterval totalOffset = /*time to add - in seconds */ NSDate *today = [NSDate date]; NSDate *endWithTZ = [today dateByAddingTimeInterval:totalOffset]; 
-1
source

There may be a better way (and I am looking at this question to see if there is), but this is what I am currently using:

 + (NSDate *)offsetFromDate:(NSDate *)fromDate withDays:(int)days { NSTimeInterval timeInterval = (days * 86400); NSDate *date = [[NSDate alloc] initWithTimeInterval:timeInterval sinceDate:fromDate]; return date; } 

If you specify Google for the number of seconds per day, you will get the value 86400.

-2
source
  NSLog(@"%@", [date dateByAddingTimeInterval:15*24*60*60]); 
-2
source

All Articles