How can i get next date using NSDate?

32,088

Solution 1

In the following, yourDate represents your input NSDate; nextDate represents the next day.

// start by retrieving day, weekday, month and year components for yourDate
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate];
    NSInteger theDay = [todayComponents day];
    NSInteger theMonth = [todayComponents month];
    NSInteger theYear = [todayComponents year];

    // now build a NSDate object for yourDate using these components
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:theDay]; 
    [components setMonth:theMonth]; 
    [components setYear:theYear];
    NSDate *thisDate = [gregorian dateFromComponents:components];
    [components release];

    // now build a NSDate object for the next day
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setDay:1];
    NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
    [offsetComponents release];
    [gregorian release];

Solution 2

NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]];

Much more easiest way..

Solution 3

what about this methods ?

http://www.drobnik.com/touch/2009/05/adding-days-to-nsdate/

NSDate *now = [NSDate date];
int daysToAdd = 50;  // or 60 :-)

// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd];

// create a calendar
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0];
NSLog(@"Clean: %@", newDate2);

Solution 4

I guess this method works just fine for me.

NSString *zajtra = [[NSDate date] dateByAddingTimeInterval:86400];

Solution 5

//Add the below code to init method or viewDidload

[[NSUserDefaults standardUserDefaults] setObject:[NSDate date]  forKey:@"Date"];

//Add this code where you wanna retrieve next or previous dates

NSDate *tomorrow = [[NSUserDefaults standardUserDefaults] valueForKey:@"Date"];
NSTimeInterval secondsPerDay = 24 * 60 * 60;    
NSDate *date = [tomorrow addTimeInterval:secondsPerDay]; //Change NSDate *date = [tomorrow addTimeInterval:-secondsPerDay]; for yesterday
tomorrow = date;
[self setDate:tomorrow];
dateTextField.text = [self getDate];        
[[NSUserDefaults standardUserDefaults] setObject:tomorrow  forKey:@"Date"]; 
Share:
32,088

Related videos on Youtube

Jai
Author by

Jai

Updated on December 11, 2020

Comments

  • Jai
    Jai over 3 years

    Possible Duplicate:
    iOS and finding Tomorrow

    How can i get next date using NSDate. Please send me the solution.

    • Oh Danny Boy
      Oh Danny Boy almost 13 years
      You should mark one of the answers below correct if you feel it answers your question.
  • Jai
    Jai almost 15 years
    Hi, Yeah this code's working Thanks a lot, Do you have any idea about how to get previous day(yesterday)?
  • rpetrich
    rpetrich almost 15 years
    Changing [offsetComponents setDay:1] to be setDay:-1 results in subtracting a day instead of adding
  • Niels Castle
    Niels Castle over 14 years
    Cave: Repeatedly adding secondsPerDay (24 * 60 * 60) with addTimeInterval to an NSDate is done while observing daylight saving time. Adding 24 hours on a day when you enter or leave daylight savin time skews the time of day by +/- 1 hour.
  • Kyle
    Kyle about 13 years
    This can cause issues when traversing day boundaries when accounting for DST.
  • progrmr
    progrmr about 13 years
    This can give incorrect results when there is a Daylight Savings Time change in the range of dates.
  • progrmr
    progrmr about 13 years
    Ditto, This can give incorrect results when there is a Daylight Savings Time change in the range of dates.
  • johnnieb
    johnnieb almost 13 years
    This also works when the next date moves from daylight savings to standard time. Try moving from 2011-11-06. Thanks @unforgiven.
  • Francesco Puglisi
    Francesco Puglisi about 12 years
    This code should crash because you are releasing components twice.
  • Massimo Cafaro
    Massimo Cafaro over 11 years
    @singingAtom, I have updated my answer, thank you.
  • Andres Kievsky
    Andres Kievsky about 11 years
    Also when there is a leap second.
  • Andres Kievsky
    Andres Kievsky about 11 years
    Will not do what you expect if there is a leap second.
  • gabriel_vincent
    gabriel_vincent almost 11 years
    Much better solution than the most upvoted in this topic
  • Julian F. Weinert
    Julian F. Weinert almost 9 years
    Why does your - [NSDate dateByAddingTimeInterval:] return a NSString?
  • Mrug
    Mrug almost 9 years
    This is not valid.. This will give date with same time after 24 hours.
  • orkoden
    orkoden almost 8 years
    Also breaks if the user travels between time zones.
  • iosCurator
    iosCurator almost 7 years
    It does not take care of the leap second as well. Hence it will break.
  • ATV
    ATV over 5 years
    Works like a breeze!
  • bshirley
    bshirley about 5 years
    also useful [NSCalendar currentCalendar]
  • Jim Holland
    Jim Holland about 3 years
    I take the points above but in some cases, e.g. when you just want a date in the future for testing, this is a simple solution.