Is there a better way to to this in ignition 7.9/8? I need a simple countdown timer formatted as h:mm:ss between two timestamps (time remaining until shift change in my case).
I was hoping to be able to accomplish this with an expression.
Is there a better way to to this in ignition 7.9/8? I need a simple countdown timer formatted as h:mm:ss between two timestamps (time remaining until shift change in my case).
I was hoping to be able to accomplish this with an expression.
secondsBetween(now(),{root.Popup Calendar.date})
formatted as h:mm:ss
->
numberFormat(hoursBetween({Root Container.Popup Calendar.date}, now()), '00') + ':' +
numberFormat(minutesBetween({Root Container.Popup Calendar.date}, now()) % 60, '00') + ':' +
numberFormat(secondsBetween({Root Container.Popup Calendar.date}, now()) % 60, '00')
or you can do conversion math on the seconds.
This isn’t very elegant looking, but it works.
hoursBetween(now(0),{Root Container.Popup Calendar.date})+':'+
(minutesBetween(now(0),{Root Container.Popup Calendar.date})-(hoursBetween(now(0),{Root Container.Popup Calendar.date})*60)) +':'+
(secondsBetween(now(0),toDate("2019-01-15 16:00:00"))-(minutesBetween(now(0),{Root Container.Popup Calendar.date})*60))
This is almost the "right" answer - slightly cleaner/better is to just use 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 the reader.
Curious why
dateFormat({[System]Gateway/CurrentDateTime},"H")
resolves (no need for toDate function) but
getHour24({[System]Gateway/CurrentDateTime})
does not? Instead I must use
getHour24(todate({[System]Gateway/CurrentDateTime}))
Just curious.
No particular reason, other than expression functions are (were) fairly rigorous in their expected types. There’s a pending change for 8.0 that will make things like that work as expected, as much as possible.
In my case, I am reading a tag from the PLC which gives me seconds.
How could I display it on a label on that format 00:00?
It basically starts reading in seconds and counts up till the tag is reset.
Could this simply be an expression added on text binding?
This can be done easily with an expression. Something like:
dateFormat(addSeconds(midnight(now()),insert your seconds tag here),"HH:mm:ss")
If you just want hours and minutes you change the time format accordingly to “HH:mm”
Not a simple expression:
concat(toInt(floor({[default]path/to/tag}/60)),
':',
numberFormat({[default]path/to/tag}%60,'00')
)
That worked perfectly. Thank you
That worked perfectly. Thank you
So If I want to Display another Label showing time how would I implement this through a script?
If your talking doing the same thing but through a script it is identical but using script functions. You would need to do a system.tag.read(tag path).value to get your seconds and store it in a variable but then you would insert your variable into the script. Then it depends where the script is to how to put it in the label.
time = system.date.format(system.date.addSeconds(system.date.midnight(system.date.now()),insert your seconds val here),"HH:mm:ss")
divmod() works well. And we now get a more modern string formatting to play with.
secondsIn = 5000
m,s = divmod(secondsIn, 60)
h,m = divmod(m, 60)
print '{hr:02d}:{mn:02d}:{sc:02d}'.format(hr=h,mn=m,sc=s)
Very clean
Question : and if I want to do this convertion into a report?
I have one integer value (seconds) that I want to convert into Time or Datetime. How do?
I have try this without success…
I am trying to use a similar script like this ut I do not understand the functionality of %60. Could you please explain that?
Its modulo: