# Need to calculate the shift number based on a past event datetime tag

**URL:** <https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926>\
**Category:** Ignition\
**Tags:** scripting, ignition81, perspective\
**Created:** [June 3, 2025, 2:42am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926 "2025-06-03T02:42:25Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anthony\_Ortiz](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anthony\_Ortiz](https://forum.inductiveautomation.com/u/Anthony_Ortiz)\
**Post date:** [June 3, 2025, 2:42am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/1 "2025-06-03T02:42:25Z")

</div>

I want to assign a value to tag "Shift" based on another daytime tag "Prod\_Datetime" from a past event and not the current time. The operator will enter the time to the prod\_Datetime tag using a datetime input object. The goal is to autofill the shift value when the event occurred. Get a value of 1 for first shift between 6am and 2 pm. Get a value of 2 for second shift between 2pm and 10 pm. Get a value of 3 for third shift between 10pm and 6 am. I don’t fully understand syntax or scope yet.

Shift tag is Integer  
Path [GSI\_LabelPrinterControl]Lines/Line 5/LeakerData/Shift

Prod\_DateTime is Datetime  
Path is [GSI\_LabelPrinterControl]Lines/Line 5/LeakerData/Prod\_DateTime  
Format is yyyy-MM-dd h:mm:ss aa can this be changed to 24hr?

I think suedo code would look something like this

```python
ProdHour = getHour24([GSI_LabelPrinterControl]Lines/Line 5/LeakerData/Prod_DateTime.value)

Shift = [GSI_LabelPrinterControl]Lines/Line 5/LeakerData/Shift.value

if ProdHour >= 06:00:00 and ProdHour < 14:00:00 then Shift = 1

elseif eventTime >= 14:00:00 and eventTime < 22:00:00 then Shift = 2

else Shift = 3

```

Again, I’m not sure on the syntax here. Not sure where the script would go. Would it be at the shift tag? at the datetime input? There are so many ways this can go and I don't want to start bad habits while learning this platform.

Respectfully,  
Anthony

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [June 3, 2025, 4:58am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/2 "2025-06-03T04:58:48Z")

</div>

If "Shift" is a global value across the entire gateway, then yes, it's a tag.  
Your pseudo code looks like a mix of python and expression. Go straight to expression using the [case](https://www.docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/logic/case) expression on the shift tag itself.

note: yyyy-MM-dd HH:mm:ss should gave you 24h format

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 3, 2025, 7:57am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/3 "2025-06-03T07:57:44Z")

</div>

If I understand correctly, you have 2 tags:

- `Prod_DateTime`
- `Shift`

`Prod_DateTime` is a memory tag that operator can write to through the interface.  
You want `Shift` to derive its value from `Prod_DateTime`.

I would make `Shift` an derived tag, with an expression using `case`. It would like like one of these:

```python
case (dateExtract({source}, 'hour'),
	6, 1,
	7, 1,
	8, 1,
	9, 1,
	10, 1,
	11, 1,
	12, 1,
	13, 1,
	14, 2,
	15, 2,
	16, 2,
	17, 2,
	18, 2,
	19, 2,
	20, 2,
	21, 2,
	3
)

case (True,
	dateExtract({source}, 'hour') >= 6 || dateExtract({source}, 'hour') < 14, 1,
	dateExtract({source}, 'hour') >= 14 || dateExtract({source}, 'hour') < 22, 2,
	3
)

```

The second one represents the logic better, but may be slightly harder to read.  
I wouldn't worry too much about it re-extracting the hour multiple times. It's not pretty, but the impact should be negligible. I don't think it's worth making an intermediary tag to hold the hours value.

---

<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:** [June 3, 2025, 8:09am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/4 "2025-06-03T08:09:58Z")

</div>

> [@Anthony\_Ortiz](#):
>
> Format is yyyy-MM-dd h:mm:ss aa

Recommended practice in Ignition is to store all timestamps as data type datetime in UTC and **not** formatted strings. Formatting is done at the last possible point before display for human consumption. This way you can handle timezones, daylight savings, etc. Note that Perspective projects are timezone aware and can (in the Project Properties) be set to display gateway timezone or browser timezone.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 3, 2025, 8:29am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/5 "2025-06-03T08:29:47Z")

</div>

> [@Transistor](#):
>
> Recommended practice in Ignition is to store all timestamps as data type datetime in UTC

I would go so far as to say that this is the _only_ way to store datetimes in _any_ application, otherwise your ability to shift between client timezones goes out the window, as well as working with daylight savings. Datetime strings are extremely fragile

---

<div class="post-metadata">

**Author:** ![Anthony\_Ortiz](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anthony\_Ortiz](https://forum.inductiveautomation.com/u/Anthony_Ortiz)\
**Post date:** [June 3, 2025, 9:19am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/6 "2025-06-03T09:19:31Z")

</div>

I ended up using a submit button under the datetime input with the following code

```python
def runAction(self, event):
	# Get the datetime from the Datetime Input component
	datetime_value = self.getSibling("datetimeInput").props.value # Replace with actual component name if different
	
	if datetime_value is not None:
	    hour = datetime_value.getHours() # Java Date object: 0–23
	
	    # Determine shift
	    if 6 <= hour < 14:
	        shift = 1
	    elif 14 <= hour < 22:
	        shift = 2
	    else:
	        shift = 3
	
	    # Write both values to tags
	    system.tag.writeBlocking([
	        "[GSI_LabelPrinterControl]Lines/Bliss Line 5/LeakerData/Prod_DateTime",
	        "[GSI_LabelPrinterControl]Lines/Bliss Line 5/LeakerData/Shift"
	    ], [
	        datetime_value,
	        shift
	    ])
	else:
	    system.perspective.print("No datetime selected")

```

To get the time I wanted I changed the datetime input.format value from 111 to YYYY-MM-DD HH:mm:ss it was the only way to get the formated value like this 2025-06-02 21:02:00

---

<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:** [June 3, 2025, 10:16am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/7 "2025-06-03T10:16:43Z")

</div>

Be careful of your year formatting.

> Make sure, you're using **"yyyy" and not "YYYY"** in your date format strings. Typically you want to use "yyyy" to represent a four-digit calendar year (e.g. "2021"), while "YYYY" represents a so called 'week year' - see below. Source: [Date is displayed with an incorrect year ('yyyy' vs 'YYYY' in date format) – CloverDX Customer Portal](https://support1.cloverdx.com/hc/en-us/articles/360018727699-Date-is-displayed-with-an-incorrect-year-yyyy-vs-YYYY-in-date-format)

In Expression Language

```python
dateFormat(
	getDate('2024', '11', '30'),
	"yyyy-MMM-dd"
)

```

returns 2024-Dec-30 - what you are hoping for.

Whereas,

```python
dateFormat(
	getDate('2024', '11', '30'),
	"YYYY-MMM-dd"
)

```

Returns the week year, 2025-Dec-30 - which may ruin your Christmas holidays. Week 1 this year started on 2024-12-30.

Jython will also give the same results. Try this in the Script Console:

```python
d = system.date.getDate(2024, 11, 31)
system.date.format(d, "YYYY-MMM-DD")
system.date.format(d, "yyyy-MMM-DD")

e = system.date.getDate(2024, 11, 31)
system.date.format(e, "YYYY-MMM-dd")
system.date.format(e, "yyyy-MMM-dd")

```

Result:

```python
u'2025-Dec-366'
u'2024-Dec-366'
u'2025-Dec-31'
u'2024-Dec-31'
>>>

```

See the Java docs for more:

> **[SimpleDateFormat (Java SE 17 & JDK 17)](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/SimpleDateFormat.html)**
>
> declaration: module: java.base, package: java.text, class: SimpleDateFormat

---

<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:** [June 3, 2025, 12:01pm UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/8 "2025-06-03T12:01:07Z")

</div>

> `datetime_value = self.getSibling("datetimeInput").props.value`

On a separate note, you can avoid all the `getSibling / Parent / Uncle / Nephew` syntax as follows:

- Create a custom property, `dateTimeInput` on the view.
- Create a binding from the DateTime input component's `value` property to `view.custom.dateTimeInput`. Make it bidirectional.
- Modify your scripts or bindings to use `view.custom.dateTimeInput`.

Now your application won't break if you rename something or move it in or out of a container.  
You'll also be able to examine all the values at once in the view property's `CUSTOM` section.

---

<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:** [June 3, 2025, 12:10pm UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/9 "2025-06-03T12:10:27Z")

</div>

> [@pascal.fragnoud](#):
>
> I don't think it's worth making an intermediary tag to hold the hours value.

If you are using my Integration Toolkit, you can use `transform()` to capture the hour in one spot. Makes the whole thing easier to read, too.

---

<div class="post-metadata">

**Author:** ![Anthony\_Ortiz](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anthony\_Ortiz](https://forum.inductiveautomation.com/u/Anthony_Ortiz)\
**Post date:** [June 3, 2025, 11:14pm UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/10 "2025-06-03T23:14:18Z")

</div>

Thanks for explainging the YYYY vs yyyy. If I use yyyy then the format result breaks, Using YYYY the format result is as desired.  
For example Using YYYY

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/c/bc0b4d875ffdd7032acf7108004819012fd66d50.png)

Using yyyy How do I resolve the resulting format error?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/d/4d5d7d580866cc363082879895eee47f3cfd7096.png)

I would also love to do it without the submit button but I think I'm binding wrong or something. I have the datetimeinput.value bidirectionally tied to my prod\_datetime tag. (which is also a datetime tag as suggested earlier. This tag is not a "now" timestamp but a chosen time in history.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/5/7593909c58b0787019b5bd5b1c636b0f65614c0b.png)

I can't tell you all how grateful I am for the support.

---

<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:** [June 3, 2025, 11:29pm UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/11 "2025-06-03T23:29:18Z")

</div>

Just to bring the joy of learning to your heart, [Perspective - DateTime Input | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/components/perspective-components/perspective-input-palette/perspective-datetime-input#properties) says,

> format   
> Template for formatting date display - must be valid moment.js format, e.g., 'MM/DD/YYYY h:mm a'.

Their docs are at [https://momentjs.com/](https://momentjs.com/). If you find a good formatting summary perhaps you could post a link here.

I suspect that this formatting is done in the browser rather than on the gateway, hence the Javascript formatting rather than the Java. We don't really have time for this, do we?

I played around with the DateTime Input component to reproduce your problem. I saw it for a moment and then, whatever I typed, it fixed itself and I can't reproduce it. That could be a problem in Designer which has to do a pile of trickery to work like a browser. Test in a real browser.

---

<div class="post-metadata">

**Author:** ![Anthony\_Ortiz](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anthony\_Ortiz](https://forum.inductiveautomation.com/u/Anthony_Ortiz)\
**Post date:** [June 3, 2025, 11:32pm UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/12 "2025-06-03T23:32:32Z")

</div>

Ok, this may be a dumb question but what do you mean by test in a browser vs designer? can you design in a browser? or do you mean run the live view from a browser? I did run the browser and I get the same error.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/c/9c9340735923a4110da793fc68e29347018e1a2b.png)

---

<div class="post-metadata">

**Author:** ![Anthony\_Ortiz](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anthony\_Ortiz](https://forum.inductiveautomation.com/u/Anthony_Ortiz)\
**Post date:** [June 4, 2025, 12:46am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/13 "2025-06-04T00:46:31Z")

</div>

So I think I have a solution When you initially bring in a Datetiminput compenent, the format is "111"

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/7/678ae5cb14d4c2f38342258366def044056dbb10.png)

So I make my target tag a 24 format instead of the Datetiminput

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/8/98c8cc4f776079034e046f77acb37e265501ec9e.png)

For some reason, I didnt think to use the Datetime component for my action, but it DOES have an action so, I move my on action script from the button to the Datetimeinput and tested.

Sure enough the onaction script in the Datetimeinput works and the shift is updated when you pic a time.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/9/89326cc74dd67238cc5abef44cef4f48a68f9113.png)

On a side note, once you change the Datetimeinput format from "111" you cant change it bock so I had to pull another one.

In the End the system works as needed and the only format change was in the target tag. I have some other issues, but I will start a new thread for those. Thanks everyone and have a good day

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 4, 2025, 6:56am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/14 "2025-06-04T06:56:52Z")

</div>

> [@Anthony\_Ortiz](#):
>
> "111"

Tha's `lll` (lower case `L`), not `111` (the digit)

---

<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:** [June 4, 2025, 9:06am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/15 "2025-06-04T09:06:08Z")

</div>

I can't help feeling that you're doing something wrong here using a tag to hold the shift number for a historical date. The only reason I can think of is that if one user changes the date then all users should see it.

If you're using it as a global variable then use SESSION CUSTOM properties, available by selecting Perspective in Project Browser and scrolling down to the bottom in the Perspective Property Editor. Doing this will isolate each session so that users don't interfere with each other.

---

<div class="post-metadata">

**Author:** ![Anthony\_Ortiz](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anthony\_Ortiz](https://forum.inductiveautomation.com/u/Anthony_Ortiz)\
**Post date:** [June 5, 2025, 11:00am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/16 "2025-06-05T11:00:45Z")

</div>

To be honest, I am probably doing a lot of things wrong here. While I am well versed in Proficy, and can get by in Wonderware, I am very new to how ignition works. I almost sent the data to a different system to process it there and just reference tags in the ignition prospective view, but that wouldn't be any fun would it? I want to learn it and my client is paying me to figure it out. I am very grateful to the support and people here. Anyway, to explain a little better there is a paper form that is used to describe details of a product that leaks. The tag Shift is for the report. It will represent when the defect occurred and is used to easily categorize the Db table by shift. It is one of many tags specifically requested to be on the form and written to the Db. I must iterate again, "me noob" lol. I got the request, and the client got ignition, so I'm getting the job done and improving as I progress, I will realize I am not very efficient at this point and will improve as I learn. Again, Thank you all for the learning experience.

Respectfully,  
Anthony

---

<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:** [June 5, 2025, 11:25am UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/17 "2025-06-05T11:25:53Z")

</div>

Yes, no need for a tag there. The date can be input using a datepicker with its value bound to a custom property on the view. (Make the binding bidirectional so the datepicker can write to it and not just read.)  
Do the same with all the other form values.  
The action or script that writes to the database then just takes all those custom props as parameters.

It might help to set up the custom property as an object with a structure like,

```python
CUSTOM
reportRecordData
  + date : 2025-06-05 14:02
  + shift: 2
  + product : Product that should not leak
  + pass: true

```

Now you can easily use these. Clients on multiple machines won't interfere with each other (which they will with your tag approach).

If you're not already, use [Named Queries | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/platform/sql-in-ignition/named-queries), Watch the video!

---

<div class="post-metadata">

**Author:** ![Anthony\_Ortiz](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anthony\_Ortiz](https://forum.inductiveautomation.com/u/Anthony_Ortiz)\
**Post date:** [June 5, 2025, 12:04pm UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/18 "2025-06-05T12:04:21Z")

</div>

Not sure if it is proper forum etiquette to chat here as this seems to be the start of a tangent I want to engage in. Is it acceptable to continue in this post or is there a chat area or some other means to continue? Is there an IRC or something for the forums?

---

<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:** [June 5, 2025, 12:05pm UTC](https://forum.inductiveautomation.com/t/need-to-calculate-the-shift-number-based-on-a-past-event-datetime-tag/106926/19 "2025-06-05T12:05:44Z")

</div>

Create a thread in the General Discussion section. I won't be available for the rest of the day but others will chip in.
