Getting the current time in an expression

I have an expression for a custom message of an alarm that looks like this:

if(left({State}, 5) = "Clear"
,
"- '{name}' on '{displayPath}' went out of alarm at " + getHour12(now()) + ":" + getMinute(now()) + ":" + getSecond(now()) + " " + if(getAMorPM(now()) = 1, "PM", "AM") + "."
,
"- '{name}' on '{displayPath}' went into alarm at " + getHour12(now()) + ":" + getMinute(now()) + ":" + getSecond(now()) + " " + if(getAMorPM(now()) = 1, "PM", "AM") + ". " + "\n" +
"   Drive Fault: " + {[.]LastFault} + " - " + {[.]LastFaultName})

I am doing this to include tag values in the email. This works great except when the minutes or seconds are less than ten. The time can end up looking something like this:

4:5:2 PM

Obviously, this does not look good. Is there a better way to grab the time part of now()? Is there a different way to do what I am doing?

https://docs.inductiveautomation.com:8443/display/DOC78/dateFormat should do the trick

Why did I not see that function?! Thanks.

Just for completeness, I’ll link the current version of the docs: https://docs.inductiveautomation.com:8443/display/DOC79/dateFormat

In 7.9 we introduced a whole lot more date handling expression functions, which you can see in the left-hand column. Dealing with dates should be relatively easy now in both expressions and functions.

1 Like