Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a day to current date in iOS

I saw this post: iOS and finding Tomorrow.

The code provided is:

units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* comps = [[NSCalendar currentCalendar] components:units fromDate:[NSDate date]]; 
// Add one day
comps.day = comps.day+1; // no worries: even if it is the end of the month it will wrap to the next month, see doc
// Recompose a new date, without any time information (so this will be at midnight)
NSDate* tomorrowMidnight = [[NSCalendar currentCalendar] dateFromComponents:comps];

Problem: I need to add a day to my current date so that for example, the difference between 21st Jun 2013 and 21st May 2013 is 1 month instead of 0 month.

The code I am using:

    NSDate *selectedDate = [picker2 date];
    unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
    NSDateComponents *conversionInfo = [currCalendar components:unitFlags fromDate:selectedDate toDate:[NSDate date]  options:0];
    DLog(@"%i",conversionInfo.month);
    DLog(@"%i",conversionInfo.day);
    conversionInfo.day +=1;
    DLog(@"%i",conversionInfo.day);
    DLog(@"%i",conversionInfo.month);
    int months = [conversionInfo month];

But when I tried to get the difference between 21st Jun 2013 and 21st May 2013 -> still returns me 0 month instead of 1 month.

Need some help on this.

like image 385
lakshmen Avatar asked Jun 21 '13 08:06

lakshmen


People also ask

How to add 1 day in date Swift?

Add Days to Current Date To add more days to a current date object, you can use the Calendar and the DateComponents() Swift structs. print(futureDate!) let currentDate = Date() var dateComponent = DateComponents() dateComponent. day = 1 let futureDate = Calendar.

How do I get the current date in Xcode?

Use NSDate and NSDateFormatter. NSDate *today = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"dd/MM/yyyy"]; NSString *dateString = [dateFormat stringFromDate:today]; NSLog(@"date: %@", dateString);

How do I get todays date in Swift?

To get only current date, containing day, month and year, use Date and DateFormatter . Date returns current point in time, while DateFormatter with specified dateFormat formats this Date value to required format, like in this case returning only date (day, month and year).


3 Answers

Form a date component with number of days you want to add to your original date. From the current calendar form the date by adding the this component to your original date.

NSDateComponents *dateComponents = [NSDateComponents new];
dateComponents.day = 1;
NSDate *newDate = [[NSCalendar currentCalendar]dateByAddingComponents:dateComponents 
                                                               toDate: selectedDate 
                                                              options:0];
like image 199
Anupdas Avatar answered Oct 15 '22 03:10

Anupdas


Here is a method I use:

+ (NSDate *)addDays:(NSInteger)days toDate:(NSDate *)originalDate {
    NSDateComponents *components= [[NSDateComponents alloc] init];
    [components setDay:days];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    return [calendar dateByAddingComponents:components toDate:originalDate options:0];
}

With it, you can add as many days as you want. It also works with negative numbers, so you can subtract days.

like image 25
Nikola Kirev Avatar answered Oct 15 '22 03:10

Nikola Kirev


Simple and straight forward solution :

NSDate *currentDate = [NSDate date];

NSDate *nextDate = [currentDate dateByAddingTimeInterval:(24*3600)]; 

This is working fine for me.

like image 28
Nirav Jain Avatar answered Oct 15 '22 03:10

Nirav Jain