# Adding two stored Times HH:mm:ss together

**URL:** <https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [September 11, 2023, 2:50pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964 "2023-09-11T14:50:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Jenkins](https://avatars.discourse-cdn.com/v4/letter/t/a5b964/32.png) [@Tom\_Jenkins](https://forum.inductiveautomation.com/u/Tom_Jenkins)\
**Post date:** [September 11, 2023, 2:50pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964/1 "2023-09-11T14:50:10Z")

</div>

I have historical data that I pulled using SQL which I I'm looping through for the accumulative time down for the multiple chutes on our system item[1]. Which I don't think I have a problem with. I have both the current alarm time and the historical alarm times. I just wanted to combine the two.

For example:

nd\_time[count2] is the current alarm duration. It is a string and contains "00:10:50"  
item[1] is the historical time it's unicode and contains "01:09:54"

If I just try adding them together it just adds the strings to "00:10:5001:09:54"  
I can convert them both to datetime.datetime like I've done below but then I get a type error when using datetime.combine:

TypeError("time argument must be a time instance")

I want to combine "00:10:50" and "01:09:54" and get "01:20:44"

```auto
	for item in history_data:
		desc2[count2] = "CHUTE_" + str(count2+1)
		if temp['UDT_tag'] == desc2[count]:
			currenttime= datetime.strptime(nd_time[count2], "%H:%M:%S")
			accutime = datetime.strptime(item[1], "%H:%M:%S")
			totaltime[count] = datetime.combine(currenttime, accutime)
			print("Item :" + totaltime[count])
		count = count+ 1

```

Seems like something simple I'm missing.

Thank you.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 11, 2023, 3:25pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964/2 "2023-09-11T15:25:46Z")

</div>

> [@Tom\_Jenkins](#):
>
> Seems like something simple I'm missing.

Don't store dates as strings?

All of that aside, I strongly recommend not using the python Datetime module, for anything. Instead use java and or Ignition's own date functions.

> [@Tom\_Jenkins](#):
>
> If I just try adding them together it just adds the strings to "00:10:5001:09:54"

Of course it does. To the interpreter these are just strings, and so the result you get is the concatenation of them.

> [@Tom\_Jenkins](#):
>
> I can convert them both to datetime.datetime like I've done below but then I get a type error when using datetime.combine:
> 
> TypeError("time argument must be a time instance")

I think you have a fundamental misunderstanding of what `datetime.combine()` actually does. It takes a date and a time and combines them into a single datetime object representing a date and time. In this case you have two times, and (I think) you want to add them together.

Okay, assuming you're stuck for some reason with strings in the database instead of datetime objects, how do you solve your issue? First convert the string time values into something that we can work with as a representation of a date.

This code will convert a string with your format into a date object.

```auto
currenttime = system.date.parse(nd_time[count2], "HH:MM:SS")
accutime = system.date.parse(item[1], "HH:MM:SS")

```

Then you can use the date functions as appropriate to determine the new date you're looking for.

```auto
totaltime[count] = system.date.addMillis(currenttime, system.date.toMillis(accutime))

```

You can then use `system.date.format()` to format the date object as a string for printing.

Here is a link to the Ignition Date functions:  
[https://docs.inductiveautomation.com/display/DOC81/system.date](https://docs.inductiveautomation.com/display/DOC81/system.date)

---

<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:** [September 11, 2023, 3:37pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964/3 "2023-09-11T15:37:19Z")

</div>

If there's a date component to the historical, I'd strongly recommend adding that into the mix as well, to better handle items going past midnight.

* * *

While we don't recommend using Jythons datetime.datetime objects, I've not yet had an issue using timedelta to put durations into an HMS format.

```python
from datetime import timedelta

def getSec(time_str):
    """Get seconds from time."""
    h, m, s = time_str.split(':')
    return int(h) * 3600 + int(m) * 60 + int(s)
    
def secToHMS(sec):
	return(timedelta(seconds=sec))

t1 = getSec("00:10:50")
t2 = getSec("01:09:54")

print secToHMS(t1+t2)

```

Output

```plaintext
 
1:20:44
>>>

```

---

<div class="post-metadata">

**Author:** ![Tom\_Jenkins](https://avatars.discourse-cdn.com/v4/letter/t/a5b964/32.png) [@Tom\_Jenkins](https://forum.inductiveautomation.com/u/Tom_Jenkins)\
**Post date:** [September 11, 2023, 3:41pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964/4 "2023-09-11T15:41:29Z")

</div>

Thank you so much. I was pretty confused by what I was trying to do. So many different date time options.

---

<div class="post-metadata">

**Author:** ![Tom\_Jenkins](https://avatars.discourse-cdn.com/v4/letter/t/a5b964/32.png) [@Tom\_Jenkins](https://forum.inductiveautomation.com/u/Tom_Jenkins)\
**Post date:** [September 11, 2023, 4:00pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964/5 "2023-09-11T16:00:10Z")

</div>

On this line

```auto
totaltime[count] = system.date.addMillis(currenttime, system.date.toMillis(accutime))

```

I get "TypeError: addMillis(): 2nd arg can't be coerced to int" when running this

---

<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:** [September 11, 2023, 4:03pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964/6 "2023-09-11T16:03:24Z")

</div>

Could probably just do:  
`totaltime[count] = currenttime.time + accutime.time`

If you just want the delta milliseconds.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 11, 2023, 4:03pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964/7 "2023-09-11T16:03:47Z")

</div>

Interesting.

If you insert a print statement prior to that line:

```auto
print system.date.toMillis(accutime),type(accutime)

```

What happens?

---

<div class="post-metadata">

**Author:** ![Tom\_Jenkins](https://avatars.discourse-cdn.com/v4/letter/t/a5b964/32.png) [@Tom\_Jenkins](https://forum.inductiveautomation.com/u/Tom_Jenkins)\
**Post date:** [September 11, 2023, 4:12pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964/8 "2023-09-11T16:12:40Z")

</div>

> [@lrose](#):
>
> If you insert a print statement prior to that line:
> 
> ```auto
> print system.date.toMillis(accutime),type(accutime)
> 
> ```
> 
> What happens?

Here's the result:

28879200058 \<type 'java.util.Date'\>

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 11, 2023, 4:15pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964/9 "2023-09-11T16:15:35Z")

</div>

I would expect it to work. Odd.

Try @paul-griffith's suggestion.

---

<div class="post-metadata">

**Author:** ![Tom\_Jenkins](https://avatars.discourse-cdn.com/v4/letter/t/a5b964/32.png) [@Tom\_Jenkins](https://forum.inductiveautomation.com/u/Tom_Jenkins)\
**Post date:** [September 11, 2023, 5:03pm UTC](https://forum.inductiveautomation.com/t/adding-two-stored-times-hhss-together/78964/10 "2023-09-11T17:03:57Z")

</div>

I used PGriffith's suggestion and it's working well. Thanks to everyone for the suggestions and quick responses.
