# Gateway Event - Time Date, add day for tomorrow

**URL:** <https://forum.inductiveautomation.com/t/gateway-event-time-date-add-day-for-tomorrow/82479>\
**Category:** General Discussion\
**Tags:** scripting\
**Created:** [November 29, 2023, 2:55pm UTC](https://forum.inductiveautomation.com/t/gateway-event-time-date-add-day-for-tomorrow/82479 "2023-11-29T14:55:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Markee](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Mike\_Markee](https://forum.inductiveautomation.com/u/Mike_Markee)\
**Post date:** [November 29, 2023, 2:55pm UTC](https://forum.inductiveautomation.com/t/gateway-event-time-date-add-day-for-tomorrow/82479/1 "2023-11-29T14:55:15Z")

</div>

I am trying to add a day when the shifts turns tomorrow. The current system has people put in the start and end time in a string format. I have it working up until I use timedelta(days=1).

HSGLHourEndTime = HSGLHourEndTime + datetime.timedelta(days=1)

If I try to the system.date.addDays, I get a java utility error.

```
MilitaryStartTime = system.tag.readBlocking('[default]353/Shift Times/Second/10 Hours/Hour 8/Start Time')[0]
MilitaryStartTime = int(MilitaryStartTime.value)
MilitaryStartHour = (MilitaryStartTime/100)
MilitaryStartMinutes = (MilitaryStartTime - (MilitaryStartHour*100))
HSGLHourStartTime = datetime_object.replace(hour = MilitaryStartHour, minute = MilitaryStartMinutes, second = 0)
#HSGLHourStartTime = HSGLHourStartTime.strftime("%Y-%m-%d %H:%M:%S")
#print MilitaryStartTime
#Set our End Times
MilitaryEndTime = system.tag.readBlocking('[default]353/Shift Times/Second/10 Hours/Hour 8/End Time')[0]
MilitaryEndTime = int(MilitaryEndTime.value)
MilitaryEndHour = (MilitaryEndTime/100)
MilitaryEndMinutes = (MilitaryEndTime - (MilitaryEndHour*100))
HSGLHourEndTime = datetime_object.replace(hour = MilitaryEndHour, minute = MilitaryEndMinutes, second = 0)
if HSGLHourEndTime < HSGLHourStartTime:
	HSGLHourEndTime = HSGLHourEndTime + datetime.timedelta(days=1)
HSGLHourStartTime = HSGLHourStartTime.strftime("%Y-%m-%d %H:%M:%S")	
HSGLHourEndTime = HSGLHourEndTime.strftime("%Y-%m-%d %H:%M:%S")
StartTime = str(HSGLHourStartTime)
EndTime = str(HSGLHourEndTime)
parameters = {"StartTime":StartTime, "EndTime":EndTime, "Department":"HS GAS"}
data = system.db.runNamedQuery('Oracle/Hour Count', parameters)
system.tag.writeBlocking('[default]353/KPI/Second Shift/Hour 8 Tank Counts', data)

```

---

<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:** [November 29, 2023, 3:03pm UTC](https://forum.inductiveautomation.com/t/gateway-event-time-date-add-day-for-tomorrow/82479/2 "2023-11-29T15:03:58Z")

</div>

> I am trying to add a day when the shifts turns tomorrow.

Can you explain this more clearly? How does a shift "turn tomorrow"?

> The current system has people put in the start and end time in a string format.

Why is it not entered in a date format? You should do everything date related in datetime data types and format it at the last possible moment prior to display.

Ignition has built-in [system.date - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/system.date) functions which should be able to do everything you want.

If you must use a string input, what is its format? You haven't given an example.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 29, 2023, 3:08pm UTC](https://forum.inductiveautomation.com/t/gateway-event-time-date-add-day-for-tomorrow/82479/3 "2023-11-29T15:08:57Z")

</div>

> [@Transistor](#):
>
> You should do everything date related in datetime data types and format it at the last possible moment prior to display.

And _ **do not** _ use jython's datetime module. _ **At all** _. Use Ignition's provided `system.date.*` functions, or native java classes/methods that yield proper `java.util.Date` objects.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 29, 2023, 3:20pm UTC](https://forum.inductiveautomation.com/t/gateway-event-time-date-add-day-for-tomorrow/82479/4 "2023-11-29T15:20:15Z")

</div>

I would say too you don't even have to format in the tag. When you're displaying it via a binding or what have you, if its a component made to work with dates you have options there to modify how it looks, and you always have [dateFormat - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/dateFormat) as a fallback.

Leave how you present the data on the presentation layer ie the vision window or perspective view.

One other benefit of using the `system.date` and java date objects is I se you are doing this

```auto
StartTime = str(HSGLHourStartTime)
EndTime = str(HSGLHourEndTime)
parameters = {"StartTime":StartTime, "EndTime":EndTime, "Department":"HS GAS"}
data = system.db.runNamedQuery('Oracle/Hour Count', parameters)

```

As long as you use `system.date.*` to make and use your dates, and then in your Named Queries define your parameters as `DateTime` objects, you do not have to worry about manually handling the stringing of the dates in SQL (thank god!). If you have made StartTime/EndTime as datetime parameters you could instead hypothetically do something like

```auto
EndTime = system.date.now()
StartTime = system.date.addHours(EndTime, -8)
parameters = {"StartTime":StartTime, "EndTime":EndTime, "Department":"HS GAS"}
data = system.db.runNamedQuery('Oracle/Hour Count', parameters)

```

And this would work for you.

---

<div class="post-metadata">

**Author:** ![Mike\_Markee](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Mike\_Markee](https://forum.inductiveautomation.com/u/Mike_Markee)\
**Post date:** [November 29, 2023, 3:53pm UTC](https://forum.inductiveautomation.com/t/gateway-event-time-date-add-day-for-tomorrow/82479/5 "2023-11-29T15:53:29Z")

</div>

Shift can be any set of hours the employees work. One Assembly line starts at 4:30pm and goes to 2:30pm. They would like to see hour counts between 4:30 and 5:30...  
When you hit 11:30pm and go to 12:30am, we are into the next day within that hour/shift. Customer is always right 🙂

---

<div class="post-metadata">

**Author:** ![Mike\_Markee](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Mike\_Markee](https://forum.inductiveautomation.com/u/Mike_Markee)\
**Post date:** [November 29, 2023, 3:56pm UTC](https://forum.inductiveautomation.com/t/gateway-event-time-date-add-day-for-tomorrow/82479/6 "2023-11-29T15:56:00Z")

</div>

Unfortunately, this is Oracle and it did not like the datetime. I agree with SQL being much easier to utilize the DateTime format.  
I could only get the queries to work from strings. I will give this a shot along with system.date, I could always just my shift UDT to give a full date and stop this date manipulation in the script.

I appreciate the help and the suggestions!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 29, 2023, 4:39pm UTC](https://forum.inductiveautomation.com/t/gateway-event-time-date-add-day-for-tomorrow/82479/7 "2023-11-29T16:39:09Z")

</div>

> [@Mike\_Markee](#):
>
> I could only get the queries to work from strings.

This is a problem. You should be passing value parameters.

Show your named query.
