# Question about "time" and coding

## Recommended Posts

I was sitting here making a clock. Which is kinda simple at start, but after some work i ran into several issues. This clock lets the user add hours to the current time. The problem that occurs is that lets say the time is 23:00 and you add 3 hours to that, the time would be 02:00. In the program the time would be 26:00 . My math knowledge (and logic thinking) is way down on the floor.

Ive tried various while loops etc, but its really complicated and im 99% sure im doing it the wrong way (wrong approach to this problem). First i have this little thing:

```\$CurrentTime = 23
\$Ugh = 0

\$CurrentTime = \$CurrentTime + \$AddedHours

While \$CurrentTime > 23
\$CurrentTime = \$CurrentTime - 1
\$Ugh = \$Ugh - 1
WEnd```

Ok, above the current time at top would be 23:00. Then i add 3 hours. In the While loop i would check how many hours above 23 i were. In the \$Ugh variable i get a negative value, but i get the "rest" sum that i need to "restart" the clock with. This is where i pretty much get stuck. \$Ugh being a negative value, i need to somehow convert it to a positive so i can start calculate with it.

In the end the \$Ugh would be used to count from 0 to 2 (2:00). If anyone can lend me a hand i would really appreciate it! .

This clock of course would need to work the "other" way around aswell (negative values, to go back in time).

Am i doing the right atm or am i totally off?

"I'm paper, rock is fine, nerf scissors!!!"

##### Share on other sites

This is a really tuff one... for sure

i think if you really want to look at this as Hours ( because your loop will be done in seconds) you should look at

@Hour

in the help file

your direction is not easy to understand

8)

##### Share on other sites

Do it like this:

```\$CurrentTime = 23

\$CurrentTime = \$CurrentTime + \$AddedHours

If \$CurrentTime > 23
\$CurrentTime = \$CurrentTime - 24
EndIf```
##### Share on other sites

going ahead per your example \$time = 2300 + 0300

if \$time >= 2400 then \$time = \$time - 2400

your making me think!

##### Share on other sites

Im using @hour, @min, @sec in my clock idea. I use them to get the "current" time, and save them as variables . When i have them saved as a variable, i can "change" them, in this case i want to add X amount of hours to increase time.

The problem tho, pops up when the time reaches 23 and i for example need to add 2 hours. In coding the time would be 25. I need to get those "rest overs" from 25, and "restart" the clock using the "rest overs", in this case the clock would be set to 1.

The rest is easy tho, when clock goes "forward" the date is increased by 1 (i can do this hehe), and the other way around. The problem is just changing the clock based on the amount of hours the user added.

Just in case i got it wrong up at top of thread.

```\$CurrentTime = @Hour
\$Ugh = 0
\$Time = 0

\$CurrentTime = \$CurrentTime + \$AddedHours

While \$CurrentTime > 23
\$CurrentTime = \$CurrentTime - 1
\$Ugh = \$Ugh + 1
WEnd

\$Time = \$Time + \$Ugh```

The above is how far ive got sofar. You might need to change the @Hour to 23 (just to get the idea of the problem!).

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

##### Share on other sites

would you or others be adding even hours 1 2 3 4 etc ?

##### Share on other sites

I could change the 1, 2, 3 etc to 01, 02, 03 if needed later. Now the biggest problem is just getting the stuff to work right

I added the \$Time and set it to 0. I then used the \$Ugh to start count up to get the time right. It seems to work for now. All this of course should be in a bigger loop, i just started with the problem first hehe

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

##### Share on other sites

Changed the script just a bit.

```\$CurrentTime = @Hour
\$Ugh = 0
\$Time = 0

\$CurrentTime = \$CurrentTime + \$AddedHours

If \$CurrentTime > 23

While \$CurrentTime > 23
\$CurrentTime = \$CurrentTime - 1
\$Ugh = \$Ugh + 1
WEnd

\$Time = \$Time + \$Ugh
EndIf```

Actually it doesnt look that hard anymore . I just thought there was maybe an easier way to do it all.

I used the _TimeToTicks(@HOUR,@MIN,@SEC) and _TicksToTime(\$EndTicks,\$Hour,\$Min,\$Sec) at start (from the help file), the problem was that it didnt restart the clock, it went above 23 hours if you used a too high value.

"I'm paper, rock is fine, nerf scissors!!!"

##### Share on other sites

By the way, there's a handy function in Date.au3 called _DateAdd. You might want to look at that.

##### Share on other sites

By the way, there's a handy function in Date.au3 called _DateAdd. You might want to look at that.

Thank you very much! it was just something like that i was looking for

(tho im glad i got it right in the end aswell hehe!)

"I'm paper, rock is fine, nerf scissors!!!"

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Recently Browsing   0 members

• No registered users viewing this page.
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...