lakshmen
lakshmen

Reputation: 29064

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.

Upvotes: 8

Views: 12921

Answers (6)

Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61774

To add any number of any components to the date, for example: 2 days or 2 months, 1 year, do the following:

Calendar.current.date(byAdding: DateComponents(month: -1), to: date)! //this create date with previous month for date.

you can initialize DateComponents with anything:

public init(calendar: Calendar? = default, timeZone: TimeZone? = default, era: Int? = default, year: Int? = default, month: Int? = default, day: Int? = default, hour: Int? = default, minute: Int? = default, second: Int? = default, nanosecond: Int? = default, weekday: Int? = default, weekdayOrdinal: Int? = default, quarter: Int? = default, weekOfMonth: Int? = default, weekOfYear: Int? = default, yearForWeekOfYear: Int? = default)

Upvotes: 0

Sudhir Bhagat
Sudhir Bhagat

Reputation: 137

First Thing,you are assigning day instead of month to NSDateComponent. Execute following code and find difference between inDate And newDate

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitMonth fromDate:inDate];

NSInteger monthFlag = 1; components.month = minuteFlag;

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

Upvotes: 0

Michael Shang
Michael Shang

Reputation: 756

For swift googlers:

NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: 1, toDate: date, options: nil)!

Upvotes: 2

Nikola Kirev
Nikola Kirev

Reputation: 3152

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.

Upvotes: 8

Nirav Jain
Nirav Jain

Reputation: 5107

Simple and straight forward solution :

NSDate *currentDate = [NSDate date];

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

This is working fine for me.

Upvotes: 3

Anupdas
Anupdas

Reputation: 10201

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];

Upvotes: 15

Related Questions