1

I am new to iPhone.

I want to find out the next date from given date based on repeat period.

For example :

I want function as follows ...

  • given date : 31'May 2011 and Repeat : Monthly given as argument then the next date should be returned 31'July 2011 (as June don't have 31st day)

  • And function should be smart enough to to calculate next leap year day also, if given date : 29'Feb 2008 and Repeat : Yearly given as argument then the next date should be returned 29'Feb 2012 (The next leap year day)

  • And so on repeat option can be one of these : Daily, Weekly(On selected day of week), Monthly, Yearly, None(No repeat at all)

Vaibhav Jani
  • 12,428
  • 10
  • 61
  • 73
SJS
  • 2,647
  • 1
  • 17
  • 34
  • 1
    this will help you. [http://stackoverflow.com/questions/1172698/how-do-i-get-the-next-and-earlier-date-of-the-current-date-in-iphone-sdk][1] [1]: http://stackoverflow.com/questions/1172698/how-do-i-get-the-next-and-earlier-date-of-the-current-date-in-iphone-sdk – Rakesh Bhatt Jul 13 '11 at 12:40

3 Answers3

3
// start by retrieving day, weekday, month and year components for yourDate
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) 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: yourDate options:0];
[offsetComponents release];
[gregorian release];

This is copied from How can i get next date using NSDate? and the credit goes to @Massimo Cafaro for this answer.

Community
  • 1
  • 1
Rakesh Bhatt
  • 4,606
  • 3
  • 25
  • 38
0

To get tomorrow's date use the dateByAddingTimeInterval method.

// Start with today
NSDate *today = [NSDate date];

// Add on the number of seconds in a day
NSTimeInterval oneDay = 60 * 60 * 24;
NSDate *tomorrow = [today dateByAddingTimeInterval:oneDay];

It's pretty simple to extend that to a week etc

NSTimeInterval oneWeek = oneDay * 7;
NSDate *nextWeek = [today dateByAddingTimeInterval:oneWeek];
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • 1
    `dateByAddingTimeInterval:` doesn't always give you the result you'd think. Going to daylight savings time (or back) will move the date more or less than one day. There could be other situations like when countries adjust their time to switch time zones (doesn't happen often, but it does actually happen this year). You should use `NSDateComponents` to be sure you get the date you think you'll get. – Morten Fast Jul 13 '11 at 12:51
  • You can hack that by always being in the center of a day when you change but you're right, it's not ideal :) And my answer handles moving by months really really badly! I'm amazed I got even one vote ;) @RakeshBhatt is more on the money I think. – deanWombourne Jul 13 '11 at 12:59
  • 1
    For daylight savings time, adding 24 hours to noon might give you the correct day, but the time will be off. As for the change of timezone, you won't even be on the correct day. Countries don't change timezone often enough for this to be a common problem, but given that it's not at all a problem when using NSDateComponents, I'd still say using `dateByAddingTimeInterval:` is a non-optimal solution for getting the next day. :) – Morten Fast Jul 13 '11 at 13:13
-1

try this :-

- (NSDate *)dateFromDaysOffset:(NSInteger)daysOffset
{
    // start by retrieving day, weekday, month and year components for yourDate
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setDay:daysOffset];
    NSDate *offsetDate = [gregorian dateByAddingComponents:offsetComponents toDate:self options:0];
    [offsetComponents release];
    [gregorian release];    

    return offsetDate;
}
max_
  • 24,076
  • 39
  • 122
  • 211