Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTimer countdown label

Tags:

iphone

nstimer

It will keep counting down and updating the current time. Anyone has any idea how to approach this problem? I don't see how the NStimer will recognize the date format like this: 20110803 23:59:59

like image 904
kevin Avatar asked Jun 25 '26 03:06

kevin


1 Answers

Look up NSDateFormatter. You can specify a format string (-[setDateFormat:])that lets you convert to AND from NSDate and NSString.

When converting back to your countdown view, you may want to use NSDateComponents and NSCalendar to get the pieces you need for your countdown label (instead of NSDateFormatter). You could do something like:

NSDateComponents *countdown = [[NSCalendar currentCalendar] 
    components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) 
      fromDate:[NSDate date] 
        toDate:expiration 
       options:0];

This will calculate all the unit differences for you (with respect to the device's currently configured calendar settings).

You can get the components back with a call like [countdown day] or [countdown hour].

like image 121
Ben Mosher Avatar answered Jun 27 '26 20:06

Ben Mosher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!