Coding endDate in script

event.source.parent.getComponent('Chart CR').startDate = system.date.addHours(now, -24)
event.source.parent.getComponent('Chart CR').endDate = now

I'm just trying to change this code so its 6am to 6am. So a crude example would be:

event.source.parent.getComponent('Chart CR').startDate = system.date.addHours(6am yesterday, -24)
event.source.parent.getComponent('Chart CR').endDate = 6am today

Try system.date.setTime.
https://docs.inductiveautomation.com/display/DOC81/system.date.setTime

Please also see Wiki - how to post code on this forum.

1 Like

How would you code this without having to change the date everyday?

I'm trying to code this to a button that you just click and the chart zooms into 6am yesterday to 6am today. No matter if its the 11th, 14th, 10pm, 8am, etc.

Code that just automatically changes the chart to show for example 2/9/2024 6am - 2/10/2024 6am

Tomorrow it will show 2/10/2024 6am - 2/11/2024 6am

For today use,

midnight = system.date.midnight(system.date.now())
event.source.parent.getComponent('Chart CR').startDate = system.date.addHours(midnight, -18)
event.source.parent.getComponent('Chart CR').endDate = system.date.addHours(midnight, 6)

To make it generic for any date returned by a date picker replace system.date.now() in the first line with the date picker's selected value.

So this worked except I had to change the numbers to get 6am to 6am

midnight = system.date.midnight(system.date.now())
event.source.parent.getComponent('Chart CR').startDate = system.date.addHours(midnight, -30)
event.source.parent.getComponent('Chart CR').endDate = system.date.addHours(midnight, -6)

1 Like

Good. As long as you understand how it works.

One thing you didn't understand: Wiki - how to post code on this forum.