Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding days to NSDate

I want add days to an NSDate. The approach below is not working. Can someone help me fix this issue?

int daysToAdd=[[appDlegateObj.selectedSkuData 
                                    objectForKey:@"Release Time"] intValue];

NSTimeInterval secondsForFollowOn=daysToAdd*24*60*60;

NSString *dateStr=[contentDic objectForKey:@"Date"];

NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];

[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSDate *dateFromString=[[NSDate alloc]init];

dateFromString=[dateFormatter dateFromString:dateStr];

[dateFormatter release];

 NSDate *date=[dateFromString dateByAddingTimeInterval:secondsForFollowOn];
like image 606
karthick Avatar asked Nov 17 '11 09:11

karthick


6 Answers

// Initialize stringified date presentation
NSString *myStringDate = @"2011-11-17";

// How much day to add
int addDaysCount = 30;

// Creating and configuring date formatter instance
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];

// Retrieve NSDate instance from stringified date presentation
NSDate *dateFromString = [dateFormatter dateFromString:myStringDate];

// Create and initialize date component instance
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:addDaysCount];

// Retrieve date with increased days count
NSDate *newDate = [[NSCalendar currentCalendar] 
                              dateByAddingComponents:dateComponents 
                                              toDate:dateFromString options:0];

NSLog(@"Original date: %@", [dateFormatter stringFromDate:dateFromString]);
NSLog(@"New date: %@", [dateFormatter stringFromDate:newDate]);

// Clean up
[dateComponents release], dateComponents = nil;
[dateFormatter release], dateFormatter = nil;

Output:

Original date: 2011-11-17
New date: 2011-12-17
like image 200
Serhii Mamontov Avatar answered Nov 08 '22 14:11

Serhii Mamontov


    NSDate *now = [NSDate date];
    int daysToAdd = 1;
    NSDate *newDate = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
like image 43
imike Avatar answered Nov 08 '22 14:11

imike


With iOS 8 and 10.9, you can now actually do this a lot easier. You can add any number of any calendar unit to a date. I declared a really small category method on NSDate to make this even faster (since I just needed adding days throughout my app). Code below.

-(NSDate *)addDaysToDate:(NSNumber *)numOfDaysToAdd {
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:numOfDaysToAdd.integerValue toDate:self options:nil];
    return newDate;
}

Key things: NSCalendarUnitDay is what I used to let it know I want to add days. You can change this to the other enum values like months or years.

Since it's a category on NSDate, I'm adding the value to self. If you don't want to declare a category, you'd just take a date in the method parameters, like:

-(NSDate *)addDays:(NSNumber *)numOfDaysToAdd toDate:(NSDate *)originalDate {
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:numOfDaysToAdd.integerValue toDate:originalDate options:nil];

    return newDate;
}

I use a NSNumber, but you can easily just use an NSInteger.

Finally, in Swift:

func addDaysToDate(daysToAdd: Int, originalDate: NSDate) -> NSDate? {
        let newDate = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.DayCalendarUnit, value: daysToAdd, toDate: originalDate, options: nil)
        return newDate
    }

If you want to get rid of the NSDate? and feel comfortable with unwrapping a possible nil (the date adding might fail), you can just return newDate! and get rid of the ? mark.

like image 3
serenn Avatar answered Nov 08 '22 15:11

serenn


You can add days to an NSDate by adding a time interval

NSDate* newDate = [date dateWithTimeInterval:3600*24*numberOfDays sinceDate:otherDate];

Although if you want to be really accurate about it, and take into account leap seconds, day light saving times and all this kind of thing you might want to use NSDateComponents as described in the Date and Time Programming Guide and Omtara's link.

like image 3
jbat100 Avatar answered Nov 08 '22 13:11

jbat100


NSDateComponents *offsetComponents = [[NSDateComponents alloc] autorelease];
[offsetComponents setDay:1];

newDate = [[[CalendarContainer sharedCalendarContainer] gregorian] dateByAddingComponents:offsetComponents toDate:currentlyDisplayedDate options:0];
like image 2
Mobile Developer Avatar answered Nov 08 '22 13:11

Mobile Developer


I suggest you review this article for a cleaner solution.

like image 2
Omtara Avatar answered Nov 08 '22 15:11

Omtara