Search code examples
objective-ciosnsdatenstimeinterval

How can I make a Date to jump every n seconds?


I want to make the date to change one day every "n" seconds, like a time machine. I have this code but nothing happens, any help will be appreciated. this is the code: no issues, no error ... no tomorrow date!

-(IBAction)jumpDate

{

NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
[dateFormater setDateFormat:@"dd MMMM yyyy  h:mm:ss"];  //dateFormater.dateStyle   =NSDateFormatterLongStyle; //USA date style MMMM-dd-yyyy
[dateFormater setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CDT"]];

NSDate *todayDate = [[NSDate alloc]init];

[gregDate setText : [dateFormater stringFromDate:todayDate]];

[NSString stringWithFormat:@"dd MMMM yyyy", dayCount];

dayCount++;

if (dayCount >= 365)

{
    dayCount = 365;

    [timerDate invalidate];

}
}
//and the timer

- (void)viewDidLoad
{

NSDateFormatter *dateFormater = [[[NSDateFormatter alloc]init]autorelease];
[dateFormater setDateFormat:@"dd MMMM yyyy"];

NSTimeInterval secondsPerDay = 86400 ; // = 24 * 60 * 60

NSDate *today = [[[NSDate alloc]init]autorelease];
NSDate *tomorrow;
tomorrow = [today dateByAddingTimeInterval:(NSTimeInterval)secondsPerDay];

 [gregDate setText : [NSString stringWithFormat:@" %@ ",tomorrow]];

 dayCount = 1;
 timerDate=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self    selector:@selector(jumpDate)userInfo:nil repeats:YES];


[super viewDidLoad];
}

Solution

  • Replace this line:

    NSDate *todayDate = [[NSDate alloc]init];
    

    with this line:

    NSDate *todayDate = [[NSDate date] dateByAddingTimeInterval:dayCount * 86400];