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"];
Related videos on Youtube
Author by
Jai
Updated on December 11, 2020Comments
-
Jai over 3 years
Possible Duplicate:
iOS and finding TomorrowHow can i get next date using NSDate. Please send me the solution.
-
Oh Danny Boy almost 13 yearsYou should mark one of the answers below correct if you feel it answers your question.
-
-
Jai almost 15 yearsHi, Yeah this code's working Thanks a lot, Do you have any idea about how to get previous day(yesterday)?
-
rpetrich almost 15 yearsChanging [offsetComponents setDay:1] to be setDay:-1 results in subtracting a day instead of adding
-
Niels Castle over 14 yearsCave: 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 about 13 yearsThis can cause issues when traversing day boundaries when accounting for DST.
-
progrmr about 13 yearsThis can give incorrect results when there is a Daylight Savings Time change in the range of dates.
-
progrmr about 13 yearsDitto, This can give incorrect results when there is a Daylight Savings Time change in the range of dates.
-
johnnieb almost 13 yearsThis also works when the next date moves from daylight savings to standard time. Try moving from 2011-11-06. Thanks @unforgiven.
-
Francesco Puglisi about 12 yearsThis code should crash because you are releasing components twice.
-
Massimo Cafaro over 11 years@singingAtom, I have updated my answer, thank you.
-
Andres Kievsky about 11 yearsAlso when there is a leap second.
-
Andres Kievsky about 11 yearsWill not do what you expect if there is a leap second.
-
gabriel_vincent almost 11 yearsMuch better solution than the most upvoted in this topic
-
Julian F. Weinert almost 9 yearsWhy does your
- [NSDate dateByAddingTimeInterval:]
return aNSString
? -
Mrug almost 9 yearsThis is not valid.. This will give date with same time after 24 hours.
-
orkoden almost 8 yearsAlso breaks if the user travels between time zones.
-
iosCurator almost 7 yearsIt does not take care of the leap second as well. Hence it will break.
-
ATV over 5 yearsWorks like a breeze!
-
bshirley about 5 yearsalso useful
[NSCalendar currentCalendar]
-
Jim Holland about 3 yearsI 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.