23

Possible Duplicate:
iOS and finding Tomorrow

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

Community
  • 1
  • 1
Jai
  • 417
  • 1
  • 5
  • 12

5 Answers5

58

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];
Massimo Cafaro
  • 25,429
  • 15
  • 79
  • 93
34
NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]];

Much more easiest way..

Paresh Rathod
  • 737
  • 7
  • 8
33

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);
progrmr
  • 75,956
  • 16
  • 112
  • 147
S.P.
  • 5,427
  • 11
  • 56
  • 83
2

I guess this method works just fine for me.

NSString *zajtra = [[NSDate date] dateByAddingTimeInterval:86400];
Bot
  • 11,868
  • 11
  • 75
  • 131
Vanya
  • 4,973
  • 5
  • 32
  • 57
0

//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"]; 
KingofBliss
  • 15,055
  • 6
  • 50
  • 72
Jai
  • 417
  • 1
  • 5
  • 12
  • 1
    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. – Niels Castle Oct 30 '09 at 08:18