Switching From Military To Standard Time

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.

1 Like

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 is a character index, not character length.

substring(string, startIndex[, endIndex])

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

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

After:

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

To complete your use case with AM/PM.

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")
)
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, then use normal datetime formatting for your presentation.

3 Likes

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

1 Like

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

3 Likes

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

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")))
)

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

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

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))

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

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

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

2 Likes

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.

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

dateFormat(
	setTime(
		now(),
		floor({value} / 10000),
		floor({value} / 100) % 100,
		{value} % 100
	),
	"hh:mm:ss a"
)
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.

7 Likes