My colleague said they used LDT to fix the problem. But in my case, the datablock is an interface that means there will be a lot of places need to be changed if I change the type from "Date_And_Time" to LDT. So how can I fix this problem?
Browse until you find the correct tag and use the arrow to import into ignition. This will create the tag and use the correct data type (if supported). Because with your method, maybe the dateTime data type is not the one you need to use. I don't have a siemens plc running for the moment so i cannot test it by myself.
#Tagpath datetime Array[8]
tagPaths = '[default]var'
#read value
results=system.tag.readBlocking(tagPaths)
#Check if results
if len(results)>0:
#Extract value for year
val = results[0].value[0]
year = ((val & 0xf0) / 16)*10 + (val & 0xf)
#Check if value is 21th century
if year <90:
year +=2000
else:
year += 1900
#Extract value for month
val = results[0].value[1]
month = ((val & 0xf0) / 16)*10 + (val & 0xf)
#Extract value for day
val = results[0].value[2]
day = ((val & 0xf0) / 16)*10 + (val & 0xf)
#Extract value for hour
val = results[0].value[3]
hour = ((val & 0xf0) / 16)*10 + (val & 0xf)
#Extract value for minute
val = results[0].value[4]
min = ((val & 0xf0) / 16)*10 + (val & 0xf)
#Extract value for seconds
val = results[0].value[5]
sec = ((val & 0xf0) / 16)*10 + (val & 0xf)
#Convert to time
from java.util import Calendar
cal = Calendar.getInstance()
cal.set(year, month-1, day, hour, min, sec)
print cal.getTime()
This is a quick code i writed in the script console. It should be quite easy to adapt it for your use. This allow only to read from the PLC not to write back.
The Siemens OPC UA server does not model Date_And_Time types as OPC UA DateTime values, but rather as the internal byte array value.
My generous guess as to why is that this value is essentially a date/time string, pre-rendered, with no fixed timezone or epoch associated with it, so they can't convert it to a UTC DateTime value without potentially converting the value to a different time than was intended.
Its been a while, on a project a few years ago, I managed to find this UDT someone made, and got it working for reading and writing to that data type. Not sure if it will fit your use case.