# Seconds to Time

**URL:** <https://tech-community.robotics.abb.com/t/seconds-to-time/10178>\
**Category:** RAPID Programming\
**Created:** [May 12, 2021, 8:02pm UTC](https://tech-community.robotics.abb.com/t/seconds-to-time/10178 "2021-05-12T20:02:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xerim](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@xerim](https://tech-community.robotics.abb.com/u/xerim)\
**Post date:** [May 12, 2021, 8:02pm UTC](https://tech-community.robotics.abb.com/t/seconds-to-time/10178/1 "2021-05-12T20:02:04Z")

</div>

I have this function that converts seconds (num) to hours and minutes (num:num).

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?

---

<div class="post-metadata">

**Author:** ![xerim](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@xerim](https://tech-community.robotics.abb.com/u/xerim)\
**Post date:** [May 13, 2021, 1:02pm UTC](https://tech-community.robotics.abb.com/t/seconds-to-time/10178/2 "2021-05-13T13:02:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmf](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/jmf/32/762_2.png) [@jmf](https://tech-community.robotics.abb.com/u/jmf)\
**Post date:** [May 13, 2021, 1:53pm UTC](https://tech-community.robotics.abb.com/t/seconds-to-time/10178/3 "2021-05-13T13:53:10Z")

</div>

Instead of DIV try minutes:= s / 60;

---

<div class="post-metadata">

**Author:** ![xerim](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@xerim](https://tech-community.robotics.abb.com/u/xerim)\
**Post date:** [May 13, 2021, 1:59pm UTC](https://tech-community.robotics.abb.com/t/seconds-to-time/10178/4 "2021-05-13T13:59:01Z")

</div>

Thanks for the suggestion, I implemented it but the issue with the missing 0 for minutes below 10 persists.

---

<div class="post-metadata">

**Author:** ![xerim](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@xerim](https://tech-community.robotics.abb.com/u/xerim)\
**Post date:** [May 13, 2021, 2:48pm UTC](https://tech-community.robotics.abb.com/t/seconds-to-time/10178/5 "2021-05-13T14:48:14Z")

</div>

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
