❔ Help C# countdown timer
I'm creating a countdown timer, I managed to make the minutes and seconds work, however I need it to have hours too but I'm not managing to set the code logic to show the hours, in my case the user type in a textbox the time and click on the button time starts running until it reaches 0, please could someone help me
30 Replies
Hours= time/24 (your code)
What units does time have here?
Hours, Minutes and Seconds
the user will type in the text box the time in seconds
Tell me, what is seconds/24?
Or what are you trying to calculate with that?
precisely this part that I don't understand, I managed to get minutes and seconds to work but the hours I couldn't
Well how many seconds do you need to display 1hr ?
3600
So where did you pull '24' from?
I don't know
I'm still a beginner
Beginner at multiplication?
You have a lot of 'if' without else. Looks very complicated
But it seems like the first thing you do is take away 1 second, correct?
So tell me in words, what is the order of checks you need to do for changing seconds, minutes, hours
Eg starting from 1h01m01s
the order is the user types in the text box a value that is counted in seconds, when I click on start automatically the label presents me with the value converted into hh:mm:ss, minutes and seconds I managed to make it work, but when the user types for example 3700 seconds it does not count the time. that is, it does not appear
this what i'm doing is a countdown timer
I don't think your seconds/minutes works anyway. But I'm not talking about that order
I'm asking about the order of checks you need to make in the code
The algorithm.
minutes and seconds work
my problem is with values above 3600 seconds
3800 sec
That's broken at 0 minutes 59s
Right?
the hours do not appear, it only increases in minutes
yes seconds to 0 return for 59
Oh ok, I see it .
Well, you know how when you're watching a digital countdown, the smallest unit moves first, then slowly it works to the left/larger things
That's the order your checks should be. You have them backwards
And since you're trying to get things working backwards, you've got an impossible combination of ifs
and how would it be correct?
Check seconds first
If minutes is LT 0 it can never be GT 0. that inner block will never be hit
ok thanks
wow very complicated
If you keep going with hours > minutes > seconds, then yes. Hence why I suggest you reverse your order
Or the alternative is just redo the initial calculation with 1 fewer seconds
can you give me an example
Of what
Can someone please help me?
use DateTime
there's DateTime.FromSeconds
it does all of that for you
and you could also do
dt -= DateTime.FromSeconds(1)
if you use thatSolved close this topic.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.