# Convert seconds to HH MM SS

**URL:** <https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739>\
**Category:** Ignition\
**Created:** [July 16, 2014, 11:40pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739 "2014-07-16T23:40:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paullys50](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paullys50/32/3324_2.png) [@Paullys50](https://forum.inductiveautomation.com/u/Paullys50)\
**Post date:** [July 16, 2014, 11:40pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/1 "2014-07-16T23:40:28Z")

</div>

I am reading a tag in my PLC which is a count down timer, in seconds.

I need to show this countdown time on a display, but I need the format to change to HH:MM:SS. How can I do this? I thought about using a expression binding and use “runScript” to run a gateway script, but I have to constantly pass the seconds to the script. the runScript expression seems like you have to pass it string text? Or can I pass a tag?

Now that v7.7 is out, I thought about adding a script on the tag directly, which would create the HH:MM:SS string each time the value changed, I would dump that into another string tag.

---

<div class="post-metadata">

**Author:** ![Paullys50](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paullys50/32/3324_2.png) [@Paullys50](https://forum.inductiveautomation.com/u/Paullys50)\
**Post date:** [July 17, 2014, 3:59am UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/2 "2014-07-17T03:59:16Z")

</div>

Alright so I came up with a solution by adding a script to the tag directly. Works, but need to put it through it’s paces. Here is the code:

```auto
   m, s = divmod(currentValue.value, 60)
	h, m = divmod(m, 60)
	hhmmss = "%d:%02d:%02d" % (h, m, s)
	system.tag.write("[.]remainHHMMSS", hhmmss) 
```

Little concerning as I added this to my UDT definition and all my SQLtags proceeded to time out for an extended period of time (fairly complex UDT with a large number of SQLtags). Definitely would be cautious doing this in a production environment.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 17, 2014, 10:52am UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/3 "2014-07-17T10:52:35Z")

</div>

datetime should be usable, for Jython 2.5+:

`from datetime import datetime, timedelta
sec = timedelta(seconds=int(currentValue.value))
d = datetime(1,1,1) + sec
hhmmss="%02d:%02d:%02d" % (d.hour, d.minute, d.second)
system.tag.write("[.]remainHHMMSS", hhmmss)
`

This is nicely adjustable to use days as well:

`from datetime import datetime, timedelta
sec = timedelta(seconds=int(currentValue.value))
d = datetime(1,1,1) + sec
ddhhmmss="%02d:%02d:%02d:%02d" % (d.day-1, d.hour, d.minute, d.second)
system.tag.write("[.]remainDDHHMMSS", ddhhmmss)
`

For anyone wanting to play with this in the Script playground:

[code]from datetime import datetime, timedelta

s=500000

sec = timedelta(seconds=int(s))  
d = datetime(1,1,1) + sec  
print(“DAYS:HOURS:MIN:SEC”)  
print("%02d:%02d:%02d:%02d" % (d.day-1, d.hour, d.minute, d.second))  
[/code]

---

<div class="post-metadata">

**Author:** ![hunterdg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hunterdg/32/67774_2.png) [@hunterdg](https://forum.inductiveautomation.com/u/hunterdg)\
**Post date:** [January 15, 2019, 2:32am UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/4 "2019-01-15T02:32:07Z")

</div>

Is there a better way to to this in ignition 7.9/8? I need a simple countdown timer formatted as h:mm:ss between two timestamps (time remaining until shift change in my case).

I was hoping to be able to accomplish this with an expression.

---

<div class="post-metadata">

**Author:** ![ryanjmclaughlin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryanjmclaughlin/32/4318_2.png) [@ryanjmclaughlin](https://forum.inductiveautomation.com/u/ryanjmclaughlin)\
**Post date:** [January 15, 2019, 12:58pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/5 "2019-01-15T12:58:49Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/6/63dd93898960c43967ee677975eb913225303d2d.png)

```auto
secondsBetween(now(),{root.Popup Calendar.date})

```

---

<div class="post-metadata">

**Author:** ![hunterdg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hunterdg/32/67774_2.png) [@hunterdg](https://forum.inductiveautomation.com/u/hunterdg)\
**Post date:** [January 15, 2019, 3:18pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/6 "2019-01-15T15:18:31Z")

</div>

> formatted as h:mm:ss

---

<div class="post-metadata">

**Author:** ![nick.schuetz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nick.schuetz/32/6160_2.png) [@nick.schuetz](https://forum.inductiveautomation.com/u/nick.schuetz)\
**Post date:** [January 15, 2019, 3:54pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/7 "2019-01-15T15:54:03Z")

</div>

> [@ryanjmclaughlin](#):
>
> secondsBetween(now(),{root.Popup Calendar.date})

-\>

```auto
numberFormat(hoursBetween({Root Container.Popup Calendar.date}, now()), '00') + ':' +
numberFormat(minutesBetween({Root Container.Popup Calendar.date}, now()) % 60, '00') + ':' +
numberFormat(secondsBetween({Root Container.Popup Calendar.date}, now()) % 60, '00')

```

or you can do conversion math on the seconds.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [January 15, 2019, 4:05pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/8 "2019-01-15T16:05:37Z")

</div>

This isn’t very elegant looking, but it works.

```auto
hoursBetween(now(0),{Root Container.Popup Calendar.date})+':'+
(minutesBetween(now(0),{Root Container.Popup Calendar.date})-(hoursBetween(now(0),{Root Container.Popup Calendar.date})*60)) +':'+ 
(secondsBetween(now(0),toDate("2019-01-15 16:00:00"))-(minutesBetween(now(0),{Root Container.Popup Calendar.date})*60))   

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 15, 2019, 4:27pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/9 "2019-01-15T16:27:27Z")

</div>

> [@nick.schuetz](#):
>
> `numberFormat(hoursBetween({Root Container.Popup Calendar.date}, now()), '00') + ':' + numberFormat(minutesBetween({Root Container.Popup Calendar.date}, now()) % 60, '00') + ':' + numberFormat(secondsBetween({Root Container.Popup Calendar.date}, now()) % 60, '00')`

This is _almost_ the "right" answer - slightly cleaner/better is to just use [`stringFormat`](https://docs.inductiveautomation.com/display/DOC79/stringFormat):

```auto
stringFormat("%02d:%02d:%02d", 
	hoursBetween({Root Container.Popup Calendar.date}, now()),
	minutesBetween({Root Container.Popup Calendar.date}, now()) % 60,
	secondsBetween({Root Container.Popup Calendar.date}, now()) % 60
)

```

Note that it's worth a disclaimer that this will "break" if you overflow 24 hours. You can solve that with an additional if statement and more complex logic, but I'll leave that for the reader.

---

<div class="post-metadata">

**Author:** ![hunterdg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hunterdg/32/67774_2.png) [@hunterdg](https://forum.inductiveautomation.com/u/hunterdg)\
**Post date:** [March 11, 2019, 7:23pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/10 "2019-03-11T19:23:20Z")

</div>

Curious why

```auto
dateFormat({[System]Gateway/CurrentDateTime},"H")

```

resolves (no need for toDate function) but

```auto
getHour24({[System]Gateway/CurrentDateTime})

```

does not? Instead I must use

```auto
getHour24(todate({[System]Gateway/CurrentDateTime}))

```

Just curious.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 11, 2019, 8:07pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/11 "2019-03-11T20:07:09Z")

</div>

No particular reason, other than expression functions are (were) fairly rigorous in their expected types. There’s a pending change for 8.0 that will make things like that work as expected, as much as possible.

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [October 15, 2019, 2:03pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/12 "2019-10-15T14:03:29Z")

</div>

In my case, I am reading a tag from the PLC which gives me seconds.  
How could I display it on a label on that format 00:00?

It basically starts reading in seconds and counts up till the tag is reset.

Could this simply be an expression added on text binding?

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [October 15, 2019, 2:17pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/13 "2019-10-15T14:17:31Z")

</div>

This can be done easily with an expression. Something like:

```auto
dateFormat(addSeconds(midnight(now()),insert your seconds tag here),"HH:mm:ss")

```

If you just want hours and minutes you change the time format accordingly to “HH:mm”

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [October 15, 2019, 2:17pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/14 "2019-10-15T14:17:32Z")

</div>

Not a simple expression:

```auto
concat(toInt(floor({[default]path/to/tag}/60)),
		':', 
		numberFormat({[default]path/to/tag}%60,'00')
		)

```

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [October 15, 2019, 2:27pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/15 "2019-10-15T14:27:27Z")

</div>

That worked perfectly. Thank you

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [October 15, 2019, 2:28pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/16 "2019-10-15T14:28:57Z")

</div>

That worked perfectly. Thank you

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [October 15, 2019, 2:30pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/17 "2019-10-15T14:30:34Z")

</div>

So If I want to Display another Label showing time how would I implement this through a script?

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [October 15, 2019, 2:35pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/18 "2019-10-15T14:35:16Z")

</div>

If your talking doing the same thing but through a script it is identical but using script functions. You would need to do a system.tag.read(tag path).value to get your seconds and store it in a variable but then you would insert your variable into the script. Then it depends where the script is to how to put it in the label.

```auto
time = system.date.format(system.date.addSeconds(system.date.midnight(system.date.now()),insert your seconds val here),"HH:mm:ss")

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [October 15, 2019, 2:55pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/19 "2019-10-15T14:55:17Z")

</div>

_divmod()_ works well. And we now get a more modern string formatting to play with.

```python
secondsIn = 5000

m,s = divmod(secondsIn, 60)
h,m = divmod(m, 60)
print '{hr:02d}:{mn:02d}:{sc:02d}'.format(hr=h,mn=m,sc=s)

```

 ![2019-10-15_10-54-21](https://us1.discourse-cdn.com/inductiveautomation/original/2X/4/4753108a0ce299e1423d7c972f29bdb2d1355718.png)

---

<div class="post-metadata">

**Author:** ![vipo](https://avatars.discourse-cdn.com/v4/letter/v/eada6e/32.png) [@vipo](https://forum.inductiveautomation.com/u/vipo)\
**Post date:** [May 6, 2021, 2:23pm UTC](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/20 "2021-05-06T14:23:34Z")

</div>

Very clean

[Next page](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739.md?page=2)
