How to use DTStructureGetTime in ST?

For a project I need to find out if time is before or after 11 AM.
I tried to read the time with DTStructureGetTime and compare the hour element of the DTStructure with 11.

The variables:

    dpgIst11Uhr : BOOL; (*Einspeisen darf ab 11 Uhr beginnen*)
	dpgDTStructureGetTime_1 : DTStructureGetTime; (*Reads the local time from the target system in DTStructure format.*)
	dpgCurrentDateTime : DTStructure; (*Aktuelle Uhrzeit der SPS als DTStructure*)
    status : UDINT;

The code in an cyclic module:

dpgDTStructureGetTime_1.enable := TRUE; 
status := dpgDTStructureGetTime_1.pDTStructure := ADR(dpgCurrentDateTime); // aktuelles Data & Time ist in CurrentDateTime Data type DTStructure

IF dpgCurrentDateTime.hour >= 11 THEN
	dpgIst11Uhr := TRUE;

Unfortunately, the structure dpgCurrentDateTime is empty, all elements are 0.
Additional, status must be as UDINT declared not as UINT as described in AS Help.

PLC: X20CP0482
AS 4.11

Any ideas what I am doing wrong?


I can’t see that the function block instance is called in the cyclic code.
I think you have to call “dpgDTStructureGetTime_1();” before using the outputs.

For example:

	dpgDTStructureGetTime_1.enable := TRUE;
	dpgDTStructureGetTime_1.pDTStructure := ADR(myDtStruct);
	IF dpgDTStructureGetTime_1.status = 0 THEN
		IF myDtStruct.hour > 11 THEN
			// do something

Best regards!
1 Like


thank you, I missed this line in the code. Now the program works.


1 Like