0
\$\begingroup\$

I am building a digital clock in C# after watching a tutorial. The code in the tutorial I need help understanding looks like this:

if (hh < 10) {
    time += "0" + hh;
}
else {
    time += hh;
}
time += ":";

Can anyone explain to me why hh < 10? Should it not be at least 12?

My end game is to make a timer that starts at 9am and stops at 5pm. Ongoing applications under development. Looking at time money management.

\$\endgroup\$
  • \$\begingroup\$ ps, "time" is a string, hh is an int representing the DateTime function. \$\endgroup\$ – Samuel Josling Nov 3 '19 at 11:36
  • \$\begingroup\$ The purpose of that code snippet seems to prefix a '0' character for any hour lower than 10 for representation. Nothing to do with the 12 hour clock cycle. \$\endgroup\$ – πάντα ῥεῖ Nov 3 '19 at 11:44
  • 3
    \$\begingroup\$ If this isn't your code, then I'm afraid this is off-topic. It's also off-topic, because we can't help with explaining how code works: we only review concrete, working code, which the OP already understands. See the help center for more information. \$\endgroup\$ – VisualMelon Nov 3 '19 at 12:34
  • 1
    \$\begingroup\$ Never mind. I fixed the solution using two different int values for time then comparing these with an if statement. \$\endgroup\$ – Samuel Josling Nov 3 '19 at 12:44
  • 2
    \$\begingroup\$ @SamuelJosling Please delete your question, it's blatantly off-topic here. \$\endgroup\$ – πάντα ῥεῖ Nov 3 '19 at 14:08
3
\$\begingroup\$

Welcome to Codereview.
As I can see you are having troubles understanding the line that adds a "0" in front of hh. Let's say hh is 9. Then, as 9 is smaller than 10, time will be "09", if hh is "10", or greater, it won't add that "0" at the beginning.
After that (time += ":"), the program will add a ":" which would represent the hours in the clock and a ":" character afterwards, like "10:" or "09:".
So, When it adds the minutes, the algorithm will use a similar if statement, like this:

if (hh < 10) {
    time += "0" + hh;
}
else {
    time += hh;
}
time += ":";
if (mm < 10) {
    time += "0" + mm;
}
else {
    time += mm;
}

at the end the time that will be shown, will look something like "09:09", and such.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.