# Get the Elapsed Time between 2 dates (now() - date)

**URL:** <https://forum.inductiveautomation.com/t/get-the-elapsed-time-between-2-dates-now-date/99175>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [November 27, 2024, 11:20am UTC](https://forum.inductiveautomation.com/t/get-the-elapsed-time-between-2-dates-now-date/99175 "2024-11-27T11:20:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [November 27, 2024, 11:20am UTC](https://forum.inductiveautomation.com/t/get-the-elapsed-time-between-2-dates-now-date/99175/1 "2024-11-27T11:20:58Z")

</div>

Hi!

I have a tag called `StartTime` and another that is called `ElapsedTime`.

The start time is saved into the first one when the process starts.

My approach to `ElapsedTime` is this one:

```python
toString(
	dateFormat(
		now() - {[.]StartTime},
		"dd'd' HH:mm:ss.SSS"
	)
)

```

As an expression tag.

However, it doesn't get the difference right. It adds a day and an hour for some reason.

_Edit: The format our client want is number of days, hours, minutes, seconds and miliseconds between now and the start time._  
_E.g. `0d 02:15:10.364`, `4d 04:10:00.000` or `324d 22:08:18.000`_

Any help is appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 27, 2024, 12:28pm UTC](https://forum.inductiveautomation.com/t/get-the-elapsed-time-between-2-dates-now-date/99175/2 "2024-11-27T12:28:27Z")

</div>

Use the millisBetween() function and do all the modulus/division operations yourself. I'd use my Integration Toolkit's `transform()` function to hold intermediate values if you must use an expression. (Jython's divmod is otherwise helpful here.)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [November 27, 2024, 12:57pm UTC](https://forum.inductiveautomation.com/t/get-the-elapsed-time-between-2-dates-now-date/99175/3 "2024-11-27T12:57:49Z")

</div>

Possibly some inspiration in this thread: [OPC Tag Value Seconds to HH:MM:SS via Expression Tag - #21 by WillMT10](https://forum.inductiveautomation.com/t/opc-tag-value-seconds-to-hhss-via-expression-tag/45544/21)

---

<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:** [November 27, 2024, 1:06pm UTC](https://forum.inductiveautomation.com/t/get-the-elapsed-time-between-2-dates-now-date/99175/4 "2024-11-27T13:06:04Z")

</div>

Another method adapted from [Paul's example](https://forum.inductiveautomation.com/t/convert-seconds-to-hh-mm-ss/8739/9)

```python
stringFormat("%02d d %02d:%02d:%02d.%02d", 
	daysBetween({[.]StartTime}, now()),
	hoursBetween({[.]StartTime}, now()) % 24,
	minutesBetween({[.]StartTime}, now()) % 60,
	secondsBetween({[.]StartTime}, now()) % 60,
	millisBetween({[.]StartTime}, now()) % 1000
)

```

---

<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:** [November 27, 2024, 2:58pm UTC](https://forum.inductiveautomation.com/t/get-the-elapsed-time-between-2-dates-now-date/99175/5 "2024-11-27T14:58:05Z")

</div>

@dkhayes117's expression works, but to get the format as you requested it would need to be this:

```python
stringFormat("%01dd %02d:%02d:%02d.%03d", 
	daysBetween({[.]StartTime}, now()),
	hoursBetween({[.]StartTime}, now()) % 24,
	minutesBetween({[.]StartTime}, now()) % 60,
	secondsBetween({[.]StartTime}, now()) % 60,
	millisBetween({[.]StartTime}, now()) % 1000
)

```

Using Phil's method without the his module would look like this:

```python
concat(
	toInt(millisBetween({[.]StartTime},now()) / 86400000),'d ', //days
	numberFormat((millisBetween({[.]StartTime},now()) / 3600000) % 24,'00'),':', // hours
	numberFormat((millisBetween({[.]StartTime},now()) / 60000) % 60, '00'), ':', // minutes
	numberFormat((millisBetween({[.]StartTime},now()) / 1000) % 60, '00'),'.', // seconds
	numberFormat(millisBetween({[.]StartTime},now()) % 1000, '000')
)

```

Using Phil's method with his module would look like this:

```python
transform(millisBetween({[.]StartTime},now()),
	concat(
		toInt(value() / 86400000),'d ', //days
		numberFormat((value() / 3600000) % 24,'00'),':', // hours
		numberFormat((value() / 60000) % 60, '00'), ':', // minutes
		numberFormat((value() / 1000) % 60, '00'),'.', // seconds
		numberFormat(value() % 1000, '000')
		)
	)

```

Phil's module comes highly recommended by myself and many others. The transform expression should make the expression much more performant when compared to it's native equivalent.

All three expressions return the same result given the same starting values.
