# Comparing time difference in HH:mm:ss

**URL:** <https://forum.inductiveautomation.com/t/comparing-time-difference-in-hhss/38122>\
**Category:** Ignition\
**Created:** [August 27, 2020, 8:41am UTC](https://forum.inductiveautomation.com/t/comparing-time-difference-in-hhss/38122 "2020-08-27T08:41:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yin.cheng](https://avatars.discourse-cdn.com/v4/letter/y/85f322/32.png) [@yin.cheng](https://forum.inductiveautomation.com/u/yin.cheng)\
**Post date:** [August 27, 2020, 8:41am UTC](https://forum.inductiveautomation.com/t/comparing-time-difference-in-hhss/38122/1 "2020-08-27T08:41:06Z")

</div>

Hi  
I have 2 DateTime tags:  
‘Before’, in the format of 2020-08-27 14:23:47  
‘After’, in the format of 2020-08-28 03:10:05

I would need to display the time difference of After minus Before in ‘HH:mm:ss’ format.  
I am currently using this script:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/b/bbb262e3b82c9e90f8d5e295e46eef72776ea648.png)

This script is working, but i would like to know if there is any smarter/shorter script to implement instead?

---

<div class="post-metadata">

**Author:** ![yin.cheng](https://avatars.discourse-cdn.com/v4/letter/y/85f322/32.png) [@yin.cheng](https://forum.inductiveautomation.com/u/yin.cheng)\
**Post date:** [August 27, 2020, 8:54am UTC](https://forum.inductiveautomation.com/t/comparing-time-difference-in-hhss/38122/2 "2020-08-27T08:54:04Z")

</div>

I have tried below second method as well:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/f/0f9c95a6aa749aa46af07905d622e13c651f68e8.png)

In terms of complexity, i feel that using second method would be better.  
But I am not sure in terms of “functions”, which would be more… efficient?

---

<div class="post-metadata">

**Author:** ![jonathan.taylor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jonathan.taylor/32/4048_2.png) [@jonathan.taylor](https://forum.inductiveautomation.com/u/jonathan.taylor)\
**Post date:** [August 27, 2020, 9:31am UTC](https://forum.inductiveautomation.com/t/comparing-time-difference-in-hhss/38122/3 "2020-08-27T09:31:15Z")

</div>

I don’t think what you’ve got will work; your calcs will give the wrong numbers.

For example, your After minute is 10 and your Before minute is 23. Ignoring hours and seconds, this is 47 minutes apart. Your calc will say 13.

If your difference will never be more than 24 Hours (and you don’t care about timezones…) you could do something like:

```auto
dateFormat(
     fromMillis({After}-{Before})
,"HH:mm:ss")

```

However - I would maintain would be much easier to do in a python script

---

<div class="post-metadata">

**Author:** ![yin.cheng](https://avatars.discourse-cdn.com/v4/letter/y/85f322/32.png) [@yin.cheng](https://forum.inductiveautomation.com/u/yin.cheng)\
**Post date:** [August 27, 2020, 9:55am UTC](https://forum.inductiveautomation.com/t/comparing-time-difference-in-hhss/38122/4 "2020-08-27T09:55:20Z")

</div>

Hi jonathan

Youre right… both my dates were 27th during the simulation, and hence i missed that out.  
I have tried your script, but i dont think it is considering the difference between 27th and 28th as well…

---

<div class="post-metadata">

**Author:** ![jonathan.taylor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jonathan.taylor/32/4048_2.png) [@jonathan.taylor](https://forum.inductiveautomation.com/u/jonathan.taylor)\
**Post date:** [August 27, 2020, 10:03am UTC](https://forum.inductiveautomation.com/t/comparing-time-difference-in-hhss/38122/5 "2020-08-27T10:03:02Z")

</div>

It is, but as I said it doesn’t take into account multiple days, or even timezones -

So using your times above it says the time difference is 13 hours, 46 mins and 18 seconds where it should only be 12:46:18 - this is because in my country we are currently +1 hour against our normal UTC offset.

Python would be much better for 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:** [August 27, 2020, 2:02pm UTC](https://forum.inductiveautomation.com/t/comparing-time-difference-in-hhss/38122/6 "2020-08-27T14:02:59Z")

</div>

> [@Convert seconds to HH MM SS](http://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/9):
>
> This is almost the “right” answer - slightly cleaner/better is to just use [stringFormat](https://docs.inductiveautomation.com/display/DOC79/stringFormat): 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…

See this thread for discussion on how to do this in expressions or Python.
