Seconds to Time
xerim
✭
I have this function that converts seconds (num) to hours and minutes (num:num).
It works great until you input a time in seconds that is less than 10 minutes into the hour, for example 5:04 or 7:09 would end up being printed as 5:4 and 7:9 respectively. I know this has something to do with the inputs being divisible by 60 so the MOD returns 0 as there is no remainder..how can I force it to add a 0 to complete the time format?
FUNC string SecondtoTime(num s)
VAR num seconds:=0;
VAR num minutes:=0;
VAR num hours:=0;
s:=Round(s);
IF s>=60 THEN
minutes:= s DIV 60;
seconds:= s MOD 60;
ELSE
seconds:=s;
ENDIF
IF minutes >= 60 THEN
hours:= minutes DIV 60;
minutes:= minutes MOD 60;
ELSE
minutes:=minutes;
ENDIF
RETURN NumToStr(hours,0) + ":" + NumToStr(minutes,0);
ENDFUNC
It works great until you input a time in seconds that is less than 10 minutes into the hour, for example 5:04 or 7:09 would end up being printed as 5:4 and 7:9 respectively. I know this has something to do with the inputs being divisible by 60 so the MOD returns 0 as there is no remainder..how can I force it to add a 0 to complete the time format?
0
Comments
-
In other words, I am looking for a RAPID command that allows me to prepend a 0..I can't seem to find anything in the manual that allows you to do this.0
-
Instead of DIV try minutes:= s / 60;0
-
Thanks for the suggestion, I implemented it but the issue with the missing 0 for minutes below 10 persists.0
-
I have found the solution now, the trick is to add another IF statement at the bottom of the function. Instead of:
RETURN NumToStr(hours,0) + ":" + NumToStr(minutes,0);
You need:
IF minutes < 10 THEN
RETURN NumToStr(hours,0) + ":" + "0" + NumToStr(minutes,0);
ELSE
RETURN NumToStr(hours,0) + ":" + NumToStr(minutes,0);
ENDIF
0
Categories
- All Categories
- 5.5K RobotStudio
- 396 UpFeed
- 18 Tutorials
- 13 RobotApps
- 297 PowerPacs
- 405 RobotStudio S4
- 1.8K Developer Tools
- 250 ScreenMaker
- 2.8K Robot Controller
- 316 IRC5
- 61 OmniCore
- 7 RCS (Realistic Controller Simulation)
- 799 RAPID Programming
- AppStudio
- 3 RobotStudio AR Viewer
- 18 Wizard Easy Programming
- 105 Collaborative Robots
- 5 Job listings