8

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.

Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • 2
    If you just want to add a day to today, then do this : `NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]];` This adds 24*60*60 seconds to given date and returns a new date. – Geek Jun 21 '13 at 08:34
  • thanks for the quick reply. worked...add as an answer.. will mark it right.. – lakshmen Jun 21 '13 at 08:37
  • 1
    I don't understand the last sentence in your question. Why should the difference between 21st Jun 2013 and 20th Jun 2013 be 1 month? – Martin R Jun 21 '13 at 09:07
  • apologies on the last part.. typed wrongly... – lakshmen Jun 21 '13 at 09:12
  • 1
    @lakesh: OK. But the difference betwen 21st Jun 2013 and 21st May 2013 *gives* one month (I tested it). The problem could be that the two dates have different times. For example, the difference between "21st Jun 2013 00:00" and "21st May 2013 00:30" gives 30 days, 23 hours, 30 minutes and **0 months**. So perhaps you have to normalize the dates to midnight first. – Martin R Jun 21 '13 at 09:22
  • Do you select only a day with the picker or day/hour/minute? What result do you expect if the current timestamp is "21st Jun 2013 10:00" and the selected date is "21st Aug 2013 9:00"? In other words, do you want to compute the difference to the *start of the current day* or the difference to the *current timestamp*? – Martin R Jun 21 '13 at 11:03

6 Answers6

15

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];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
8

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.

Nikola Kirev
  • 3,152
  • 3
  • 22
  • 30
3

Simple and straight forward solution :

NSDate *currentDate = [NSDate date];

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

This is working fine for me.

Nirav Jain
  • 5,088
  • 5
  • 40
  • 61
  • 3
    A day does not always have 86400 seconds. This calculation can give wrong results when transitioning to or from Daylight Savings Time. - (The same answer was already given and deleted later.) – Martin R Jun 21 '13 at 09:04
2

For swift googlers:

NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: 1, toDate: date, options: nil)!
Michael Shang
  • 686
  • 8
  • 9
0

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];
Sudhir Bhagat
  • 129
  • 1
  • 4
0

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)
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358