# Switching From Military To Standard Time

**URL:** <https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624>\
**Category:** Ignition\
**Tags:** scripting, perspective\
**Created:** [July 23, 2024, 6:56pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624 "2024-07-23T18:56:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AydHA](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@AydHA](https://forum.inductiveautomation.com/u/AydHA)\
**Post date:** [July 23, 2024, 6:56pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/1 "2024-07-23T18:56:58Z")

</div>

I have a tag that's going into a label. It is military time and need it to convert to be converted to military time.

The tag is formatted hhddss, I am trying to get it to format as hh:dd:ss AM/PM... the closest I have gotten is

concat(substring({value}, 0, 2), ":", substring({value}, 2, 2), ":",substring({value}, 4, 2) )

The value is a INT, the value could be something like 135428 -\> 1:54:28 PM or 105236 -\> 10:52:36 AM.

I feel like it has to be a really simple expression change, but I am just missing that last piece.

---

<div class="post-metadata">

**Author:** ![tyler.bennett](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tyler.bennett](https://forum.inductiveautomation.com/u/tyler.bennett)\
**Post date:** [July 23, 2024, 7:44pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/2 "2024-07-23T19:44:39Z")

</div>

> [@AydHA](#):
>
> I am trying to get it to format as hh:dd:ss AM/PM

Your use of `hh:dd:ss` instead of `hh:mm:ss` threw me off for a bit.

The third parameter of the [expression language substring function](https://www.docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/string/substring) is a character index, not character length.

```python
substring(string, startIndex[, endIndex])

```

Try this change where each `endIndex` is two greater than the corresponding `startIndex`.  
Before:

```python
concat(substring({value}, 0, 2), ":", substring({value}, 2, 2), ":",substring({value}, 4, 2) )

```

After:

```python
concat(substring({value}, 0, 2), ":", substring({value}, 2, 4), ":",substring({value}, 4, 6) )

```

---

<div class="post-metadata">

**Author:** ![tyler.bennett](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tyler.bennett](https://forum.inductiveautomation.com/u/tyler.bennett)\
**Post date:** [July 23, 2024, 7:56pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/3 "2024-07-23T19:56:02Z")

</div>

To complete your use case with AM/PM.

```python
concat(
	numberFormat(toInteger(substring({value}, 0, 2)) % 12,"#0")	
	, ":"
	, numberFormat(toInteger(substring({value}, 2, 4)),"00")	
	, ":"
	, numberFormat(toInteger(substring({value}, 4, 6)),"00")	
	, " "
	,if(toInteger(substring({value}, 0, 2)) >= 12, "PM", "AM")
)

```

```plaintext
135428 -> 1:54:28 PM
05236 -> 10:52:36 AM

```

Though if at all possible I would suggest that you convert the strings and save the dates as to a tag with [DateTime tag data type](https://www.docs.inductiveautomation.com/docs/8.1/platform/tags/tag-data-types), then use normal datetime formatting for your presentation.

---

<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:** [July 23, 2024, 8:02pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/4 "2024-07-23T20:02:31Z")

</div>

Storing dates is definitely nicer to work with; see this thread for the equivalent display function using dates:

> [@Convert seconds to HH MM SS](https://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…

---

<div class="post-metadata">

**Author:** ![matt.eaton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matt.eaton/32/72046_2.png) [@matt.eaton](https://forum.inductiveautomation.com/u/matt.eaton)\
**Post date:** [July 23, 2024, 9:32pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/5 "2024-07-23T21:32:27Z")

</div>

I suggest you join the military for a short period of time. They will yell at you until you only consider _military time_ as the only time.  
This post at 1532

---

<div class="post-metadata">

**Author:** ![AydHA](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@AydHA](https://forum.inductiveautomation.com/u/AydHA)\
**Post date:** [July 24, 2024, 1:21pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/6 "2024-07-24T13:21:25Z")

</div>

Thank you, you helped me get to the solution.

Just had to add an if statement for if the tag was displaying "71513" since that was a length of 5 instead of 6.

Really appreciate the help

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/5/751422d9e1b775f3d067c159f904ee94743b570d.png)

if({value} \< 99999,  
(concat(numberFormat(toInteger(substring({value}, 0, 1)) % 12,"#0"), ":", numberFormat(toInteger(substring({value}, 1, 3)),"00"), ":", numberFormat(toInteger(substring({value}, 3, 5)),"00"))),  
(concat(numberFormat(toInteger(substring({value}, 0, 2)) % 12,"#0"), ":", numberFormat(toInteger(substring({value}, 2, 4)),"00"), ":", numberFormat(toInteger(substring({value}, 4, 6)),"00")))  
)

---

<div class="post-metadata">

**Author:** ![AydHA](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@AydHA](https://forum.inductiveautomation.com/u/AydHA)\
**Post date:** [July 24, 2024, 1:32pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/7 "2024-07-24T13:32:59Z")

</div>

😂 I would love to, unfortunately the Bossman finds military time more confusing

---

<div class="post-metadata">

**Author:** ![AydHA](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@AydHA](https://forum.inductiveautomation.com/u/AydHA)\
**Post date:** [July 24, 2024, 7:18pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/8 "2024-07-24T19:18:35Z")

</div>

Noticed that at hh = 12 then the time is equal to 0... adjusted the code to check for that

````python
if({value} < 99999,
(concat(numberFormat(toInteger(substring({value}, 0, 1)) % 12,"#0"), ":", numberFormat(toInteger(substring({value}, 1, 3)),"00"), ":", numberFormat(toInteger(substring({value}, 3, 5)),"00"))),
(concat(if(((numberFormat(toInteger(substring({value}, 0, 2)) % 12,"#0")) > 0), (numberFormat(toInteger(substring({value}, 0, 2)) % 12,"#0")), (12)), ":", numberFormat(toInteger(substring({value}, 2, 4)),"00"), ":", numberFormat(toInteger(substring({value}, 4, 6)),"00")))
)```

//added this statement below... could be done with better logic, but this works well enough

if(((numberFormat(toInteger(substring({value}, 0, 2)) % 12,"#0")) > 0), (numberFormat(toInteger(substring({value}, 0, 2)) % 12,"#0")), (12))
````

---

<div class="post-metadata">

**Author:** ![tyler.bennett](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tyler.bennett](https://forum.inductiveautomation.com/u/tyler.bennett)\
**Post date:** [July 24, 2024, 10:49pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/9 "2024-07-24T22:49:38Z")

</div>

@AydHA, perhaps consider this version which I find easier to read and maintain (personal taste).

```python
dateFormat(
	toDate(
		concat(
			"1970-01-01 ",
			if(len({value}) = 5,
				concat(
					"0", substring({value}, 0, 1), ":",
					substring({value}, 1, 3), ":",
					substring({value}, 3, 5)
				),
				concat(
					substring({value}, 0, 2), ":",
					substring({value}, 2, 4), ":",
					substring({value}, 4, 6)
				)
			)
		)
	),
	"hh:mm:ss a"
)

```

INPUT --\> OUTPUT

```plaintext
71513 --> 07:15:13 AM
121513 --> 12:15:13 PM
001513 --> 12:15:13 AM
01513 --> 12:15:13 AM

```

@AydHA FYI when contributing multi-line code or preformatted text to the forums it is a good idea to lead and follow them them with three backticks on their own lines. This shows your preformatted code with monospaced font and (in some cases) syntax highlighting. Alternatively select your text and press (CTRL+E) or use the preformatted text `</>` button from the toolbar  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/2/52d7eb9149d93c0760100807ccf51c8f5650f9c7.png)

---

<div class="post-metadata">

**Author:** ![AydHA](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@AydHA](https://forum.inductiveautomation.com/u/AydHA)\
**Post date:** [July 25, 2024, 1:50pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/10 "2024-07-25T13:50:27Z")

</div>

Thank you, I appreciate you showing me how to format the code, cause my replies and posts I've always found a bit unreadable.

I like your updated expression, I think since what I have currently works I won't be changing the current expression. But certainly I'll be taking your better format for the future.

---

<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:** [July 25, 2024, 4:46pm UTC](https://forum.inductiveautomation.com/t/switching-from-military-to-standard-time/93624/11 "2024-07-25T16:46:30Z")

</div>

I prefer the mathematical approach as I think it makes the expression much more concise.

```python
dateFormat(
	setTime(
		now(),
		floor({value} / 10000),
		floor({value} / 100) % 100,
		{value} % 100
	),
	"hh:mm:ss a"
)

```

```plaintext
71513 --> 07:15:13 AM
121513 --> 12:15:13 PM
001513 --> 12:15:13 AM
01513 --> 12:15:13 AM
1513 --> 12:15:13 AM

```

The expression doesn't need to account for the "length" of the integer, but rather always assumes that values in the ones and tens place are seconds, hundreds and thousands are minutes, etc...

No need for string conversions, substrings, or concatenations.

Uses `now()` for the date part, but since that is being extracted in the formatting shouldn't matter.
