# Coding endDate in script

**URL:** <https://forum.inductiveautomation.com/t/coding-enddate-in-script/85525>\
**Category:** Ignition\
**Tags:** ignition78\
**Created:** [February 10, 2024, 4:11pm UTC](https://forum.inductiveautomation.com/t/coding-enddate-in-script/85525 "2024-02-10T16:11:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![water2](https://avatars.discourse-cdn.com/v4/letter/w/3da27b/32.png) [@water2](https://forum.inductiveautomation.com/u/water2)\
**Post date:** [February 10, 2024, 4:11pm UTC](https://forum.inductiveautomation.com/t/coding-enddate-in-script/85525/1 "2024-02-10T16:11:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 10, 2024, 4:24pm UTC](https://forum.inductiveautomation.com/t/coding-enddate-in-script/85525/2 "2024-02-10T16:24:18Z")

</div>

Try `system.date.setTime`.  
[https://docs.inductiveautomation.com/display/DOC81/system.date.setTime](https://docs.inductiveautomation.com/display/DOC81/system.date.setTime)

Please also see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

---

<div class="post-metadata">

**Author:** ![water2](https://avatars.discourse-cdn.com/v4/letter/w/3da27b/32.png) [@water2](https://forum.inductiveautomation.com/u/water2)\
**Post date:** [February 10, 2024, 4:49pm UTC](https://forum.inductiveautomation.com/t/coding-enddate-in-script/85525/3 "2024-02-10T16:49:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 10, 2024, 5:43pm UTC](https://forum.inductiveautomation.com/t/coding-enddate-in-script/85525/4 "2024-02-10T17:43:43Z")

</div>

For today use,

```auto
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.

---

<div class="post-metadata">

**Author:** ![water2](https://avatars.discourse-cdn.com/v4/letter/w/3da27b/32.png) [@water2](https://forum.inductiveautomation.com/u/water2)\
**Post date:** [February 10, 2024, 5:55pm UTC](https://forum.inductiveautomation.com/t/coding-enddate-in-script/85525/5 "2024-02-10T17:55:12Z")

</div>

> [@Transistor](#):
>
> `midnight = system.date.midnight(system.date.now())`

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)

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 10, 2024, 6:03pm UTC](https://forum.inductiveautomation.com/t/coding-enddate-in-script/85525/6 "2024-02-10T18:03:59Z")

</div>

Good. As long as you understand how it works.

One thing you didn't understand: [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).
