HMI Programming: Adding and Subtracting Time, Timestamps comparison

In HMI, there’s no variable type of time or datetime. It’s not possible to add and subtract time or compare two timestamps directly. Lucky we have the script function “StAndFtChange”, which would make our life much easier.

To add seconds, hours or minutes even days onto a timestamp, you just need to use “StAndFtChange” to change the timestamp to seconds since midnight of 01/01/1970.

StAndFtChange(“@W_HSW28” , “@W_HDW0”,0)

The above sentence would change current time to seconds since midnight of 01/01/1970, and save it to HDW0 (32bit addresses, HDW0 and HDW1 are used). Then add or subtract the amount of time you need to get a new value. The following code snippet adds 2 minutes on top and save it back to HDW0 and HDW1.

dim tmp as integer
tmp = @W_HDW0 + @W_HDW1<<16

tmp = tmp + 2 * 60

@W_HDW0 = tmp
@W_HDW1 = tmp>>16

Then you can use a line as follows to change it back to a new timestamp. After it, year of the new timestamp is saved to HDW10, month to HDW11, day to HDW12, hour to HDW13, minute to HDW14, second to HDW15.

StAndFtChange(“@W_HDW10” , “@W_HDW0”,1)

To compare two timestamps, you can just turn both to seconds since midnight of 01/01/1970, then compare the two values. This is another way to use a timer on HMI.