new
new

Reputation: 21

Increment Nsdate

I need to increment the NSDATE by one day every time a loop runs.I need that date to be stored inside core data.can anyone tell me how it can be done?The dates should be stored inside nsdictionary and all the contents should be should in nsdictionary

Upvotes: 1

Views: 1122

Answers (2)

yuji
yuji

Reputation: 16725

BPDeveloper's solution works, except during daylight savings changeovers where relevant. For example, if you're in any US timezone and currentDate is 12:30 am on November 6, 2011, then adding 24 hours to that time actually gives you 11:30pm on November 6, 2011 because of the daylight savings changeover.

Here's an alternative solution that avoids that problem:

NSDate *currentDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate: yourDate options:0];

Upvotes: 4

LuckyLuke
LuckyLuke

Reputation: 49097

You could use something like this:

NSDate *currentDate = [NSDate date]; // This could of course be whatever date you want.
NSDate *newDate = [[currentDate] dateByAddingTimeInterval:3600 * 24];

Upvotes: -1

Related Questions