I am using a UIDatePicker in the countdown timer mode. I want to take the current set time on the uidatepicker in seconds and put it in an integer value? How is this done?
Asked
Active
Viewed 5,999 times
3
-
1The answer to that is described here: http://stackoverflow.com/questions/10999575/uipickerview-that-looks-like-uidatepicker-but-with-seconds – Joey Clover Mar 11 '14 at 14:33
-
yeay that doesn't work for me as thats retrieving values in an array that populated that picker view.. I'm working with a uidatepicker – Mar 11 '14 at 14:41
1 Answers
6
The UIDatePicker
has a property called countDownDuration
so you should be able to use
int seconds = (int)datePicker.countDownDuration;
Edit: to address concern in the comment, make sure to manually set either the countDownDuration
or the time of the datePicker in order to get a "whole-minute" value. For instance, in viewDidLoad
you could set:
datePicker.countDownDuration = 60.0f;
and then the default-selected time duration in your countdown would be exactly 0 hours, 1 minute. Otherwise, by default the date picker will use the current date/time and setting your countdown timer could result in countDownDuration
being up to +59 seconds (e.g. 1 minute could ready anywhere from 60-119, 2 minutes from 120-179, etc) based on the time when you run your app.

Stonz2
- 6,306
- 4
- 44
- 64
-
Whoops not sure how I missed that in the class reference thanks great answer. One last question though how come it returns the correct amount + 5 though. So one minutes returns 65. 2 minutes returns 125. 1 hour return 3605. and so on – Mar 11 '14 at 19:42
-
I know I can easily just subtract 5 seconds just odd and would like to know the reason. My code is exactly how you have it in your answer – Mar 11 '14 at 19:43
-
2You have to manually set the initial time or countDownDuration value in the picker. If you leave it to default, it will set it to the current time (whenever you ran your test it must have been 5 seconds past the current minute). If you want the default-selected countdown value to be 1 minute, you could set `datePicker.countDownDuration = 60` in your `viewDidLoad` or similar method. – Stonz2 Mar 11 '14 at 20:20
-
thanks. Also with this seconds integer can I set the countdownTimer selected time when the view loads? so if I have 3600 seconds in my seconds int I would like 1 Hour 0 min selected on the timer? – Mar 11 '14 at 20:32
-
1Correct. If you set it to 3660, it will load your countdown picker with 1 hour, 1 min. If you set it to a partial minute, it will round down, so if you set it to 3659, the picker will show 1 hour, 0 min. The maximum you can set is 86399 (23 hours, 59 minutes, 59 seconds) and it will display as 23 hours, 59 min. – Stonz2 Mar 11 '14 at 21:10
-
so how is this done? How do I set the selected hour and min on the timer when the view loads? – Mar 11 '14 at 21:14
-
1Like you said, and like the post details, you set the countDownDuration property to ((hours * 3600) + (minutes * 60)) when the view loads (or whenever you want to set it). So if, when the view loads, you want the countdown timer to be automatically set to 1 hour, 7 minutes, in `viewDidLoad` set `datePicker.countDownDuration = 4020;` which is `((1 * 3600) + (7 * 60))` (4020 seconds) – Stonz2 Mar 11 '14 at 21:37
-
oh wow long day. I was thinking about it all wrong. Thank you so much with your patience! – Mar 11 '14 at 22:13