GetDate + Calculation not working

I just wrote a piece of code to return the time of the beginning of the shift and this line is giving me problems:

GetDate(GetYear(now()), GetMonth(now()), GetDayofMonth(now())-1)

If I just leave this:

GetDate(GetYear(now()), GetMonth(now()), GetDayofMonth(now()))

it works, but I need the day to be current date minus 1. Why is this not possible? How do I go around doing this?

This is the full code (but only that line is the problem):

Switch(
if(getHour24(now()) >= 8 && getHour24(now()) < 20, 1, 2),
1,2,
addHours(GetDate(GetYear(now()), GetMonth(now()), GetDayofMonth(now())), 8),
Switch(
if(getHour24(now()) >= 20, 3, 4),
3, 4,
addHours(GetDate(GetYear(now()), GetMonth(now()), GetDayofMonth(now())), 20),
addHours(GetDate(GetYear(now()), GetMonth(now()), GetDayofMonth(now())-1), 20),
GetDate(GetYear(now()), GetMonth(now()), GetDayofMonth(now()))),
GetDate(GetYear(now()), GetMonth(now()), GetDayofMonth(now())))

Basically it checks which shift is it and returns the time of the beginning of the shiift. The second switch checks if it passed midnight and if it did, it should return current date-1 (so previous day plus 20 hours since the shift started at 20:00)

Thanks

Maybe you should try a different approach that is easier to handle? Maybe create a tag that holds the shift start time for each shift. Then another tag that determines what the current shift is, then based on the current shift, choose the corresponding shift start tag.

But I will always have to have “Current date -1”. Is there a reason why this doesn’t work?

How can I get a datestamp with current date -1 + 20 hours? Is there a different approach?

Maybe try this. I have some similar tags for 2 shifts. In an expression tag named ‘Shift’ I have the following code

if(getHour24(now())>=16 || getHour24(now())<3, 2, 1)

This labels the current shift. if the current hour is between 4 pm and 3am, it is second shift = 2. Then you could have a tag event script on ValueChanged that sets the current time as the shift start time. This would eliminate some calculations. So when shift changes from 1 to 2, write the current time to the a ‘2ndShiftStart’ tag, which is just a memory tag.

Also if you want to use another method, try the dateArithmetic() function in expression language

1 Like

That idea of yours was good but I got the message after I fixed it. I ended up, creating 3 tags for GetDayofMonth, GetMonth, GetYear and after I used those 3 tags in GetDate it worked. GetDate doesn’t allow mixing, I couldn’t use a tag and a simple integer. Either all 3 integers, all three tags etc…

1 Like

There are a number of situations where integers in expressions get interpreted as “long” values, which then propagate through your arithmetic. Try wrapping integer constants in the toInt() function to force the datatype.

1 Like