Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a countdown from the current date to a specific NSDate?

I want to have a countdown from the current time to a specific date and display that value in a label. I looked at some NSTimer tutorials but I could not figure out how to apply them to my situation.

NSTimeInterval TimeInterval = [aString doubleValue]; 
NSDate* upperDate = [aDate dateByAddingTimeInterval:TimeInterval];
NSDate* Today = [NSDate date];

//cell.myLabel.text = here i should write a countdown.

Sorry for insufficient code. I usually try to write some of my own code before asking a question here but this time I could not figure what to write.

Edit So with the answer of PartiallyFinite i figured how i set timer. But because i am using tableview i could not implement the repeat message for MyTimerLabel. Here what i just did:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    MekanListesiViewCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }


    aClass *aC = [myArray objectAtIndex:indexPath.row];
    NSTimeInterval TimeInterval = [aC.aTimeIntervalwithString doubleValue];
    NSDate* UpperDate = [aC.aNSDate dateByAddingTimeInterval:TimeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:[NSDate date] toDate:UpperDate options:0];
    NSInteger days     = [dateComponents day];
    NSInteger months   = [dateComponents month];
    NSInteger years    = [dateComponents year];
    NSInteger hours    = [dateComponents hour];
    NSInteger minutes  = [dateComponents minute];
    NSInteger seconds  = [dateComponents second];
    NSString *countdownText = [NSString stringWithFormat:@"%d Days %d:%d:%d", days, hours, minutes, seconds];
    cell.countdownText= countdownText;
    [self performSelector:@selector(updateCoundown)]; // The delay is in seconds, make it whatever you want.


    return cell;
}
At myCellView.h
@interface MekanListesiViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *MyTimerLabel;
@property(weak)NSString *countdownText;
at myCellView.m

-(void)updateCoundown{

       MyTimerLabel.text = countdownText;
    [self performSelector:@selector(updateCoundown) withObject:nil afterDelay:1];
}

i get nothing in the MyTimerLabel.

like image 838
user2393702 Avatar asked May 20 '13 08:05

user2393702


3 Answers

Using the code from this answer (copy pasted below for completeness of example) to get the individual components of the countdown period:

- (void)updateCountdown {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *startingDate = [dateFormatter dateFromString:@"2005-01-01"];
    NSDate *endingDate = [NSDate date];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];

    NSInteger days     = [dateComponents day];
    NSInteger months   = [dateComponents month];
    NSInteger years    = [dateComponents year];
    NSInteger hours    = [dateComponents hour];
    NSInteger minutes  = [dateComponents minute];
    NSInteger seconds  = [dateComponents second];

We can then create a string with all of these numbers:

    NSString *countdownText = [NSString stringWithFormat:@"%d Years %d Months %d Days %d Hours %d Minutes %d Seconds", days, months, years, hours, minutes, seconds];
    cell.myLabel.text = countdownText;

Then, we can use performSelector:withObject:afterDelay: to make this method get called again after the specified delay (note that the delay is in seconds):

    [self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1];
}
like image 121
Greg Avatar answered Nov 15 '22 20:11

Greg


Declare member variables like NSDate *startDate,*EndDate; unsigned long countDownSeconds;

According to your requirement

-(void)setUpCountDown{
    startDate = [NSDate date]; //Current time
    NSDate *endDate = [startDate dateByAddingTimeInterval:10000]; //some future date
    NSTimeInterval milliseconds = [endDate timeIntervalSinceDate:startDate];/////will give time in milliseconds
    countDownSeconds = (unsigned long)milliseconds/1000;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
}

-(void)countDown:(NSTimer *)timer{
    if (countDownSeconds<=0) {
        [timer invalidate];
        timer = nil;
    }
    NSLog(@"Time Elapsed in seconds %d", countDownSeconds);
    countDownSeconds--;
}
like image 33
IronMan Avatar answered Nov 15 '22 19:11

IronMan


let formatter = NSDateFormatter()
let userCalendar = NSCalendar.currentCalendar()
let requestedComponent: NSCalendarUnit = [
    //NSCalendarUnit.Month,
    //NSCalendarUnit.Day,
    NSCalendarUnit.Hour,
    NSCalendarUnit.Minute,
    NSCalendarUnit.Second

]
func printTime() {
    formatter.dateFormat = "MM/dd/yy hh:mm:ss a"
    let startTime = NSDate()
    let endTime = formatter.dateFromString("12/25/16 8:00:00 a")
    let timeDifference = userCalendar.components(requestedComponent, fromDate: startTime, toDate: endTime!, options: [])
    self.TimeLabel.text = " \(timeDifference.hour)Hours \(timeDifference.minute)Minutes \(timeDifference.second) Seconds"
}

//Put this in your initialiser

let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(myProject.printTime), userInfo: nil, repeats: true)
    timer.fire()

I know this thread is very old, but this is how i would do it in Swift

like image 21
Milap Jhumkhawala Avatar answered Nov 15 '22 18:11

Milap Jhumkhawala