Date and time conversion

Date and time conversion 1:

Undefined result if the value range is exceeded

If the input value of a type conversion operator is outside the value range of the output data type, the result of the operation is not defined and depends on the platform. This is the case, for example, when a negative operand value is converted from LREAL to the target data type UINT. An exception error is also possible!

Date and time conversion 2:

Loss of information possible

If a larger data type is converted to a smaller data type, information may be lost.

Date and time conversion 3:

String manipulation when converting to STRING or WSTRING

With a type conversion to STRING or WSTRING, the typed value is stored as a left aligned string and truncated if it is overlong. Therefore, declare the return variables for the type conversion operators <type>_TO_STRING and <type>_TO_WSTRING long enough to accommodate the string without manipulation.

TIME/TOD_TO_<type>

The operators are used to convert a date and time specification to another data type.

You can specify a variable or a literal as the operand.

Syntax:

DATE_TO_<Datentyp> (<Variablenname> | <Literal>)
DT_TO_<Datentyp> (<Variablenname> | <Literal>)
TOD_TO_<Datentyp> (<Variablenname> | <Literal>)
LDATE_TO_<Datentyp> (<Variablenname> | <Literal>)
LDT_TO_<Datentyp> (<Variablenname> | <Literal>)
LTOD_TO_<Datentyp> (<Variablenname> | <Literal>)

The DATE and DT data types use the same internal storage format and are stored as DWORD. The resolution for DATE is 1 day. The resolution for DT is 1 second. Both begin on January 1, 1970. TOD is stored as a DWORD with a resolution of 1 millisecond.

Examples:

ST code

Result

sVar := TIME_TO_STRING(T#12ms);

'T#12ms'

nVar := TIME_TO_DWORD(T#5m);

300000

nVar := TOD_TO_SINT(TOD#00:00:00.012);

12

Converting date and time to BOOL

Operators:

DATE_TO_BOOL (<Variablenname> | <Literal>)
DT_TO_BOOL (<Variablenname> | <Literal>)
TOD_TO_BOOL (<Variablenname> | <Literal>)
LDATE_TO_BOOL (<Variablenname> | <Literal>)
LDT_TO_BOOL (<Variablenname> | <Literal>)
LTOD_TO_BOOL (<Variablenname> | <Literal>)

The operator returns a FALSE exactly at the time when the operand value can be interpreted as “0”.

Examples:

ST code

Result

DATE_TO_BOOL(D#1970-1-1);

FALSE

DT_TO_BOOL(DT#1970-1-1-0:0:0);

FALSE

TOD_TO_BOOL(TOD#0:0:0);

FALSE

DATE_TO_BOOL(D#2019-9-1);

TRUE

DT_TO_BOOL(DT#2019-9-1-12:0:0);

TRUE

TOD_TO_BOOL(TOD#12:0:0);

TRUE

Converting date and time to integer

Operators:

DATE_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
DT_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
TOD_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
LDATE_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
LDT_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
LTOD_TO_<INT-Datentyp> (<Variablenname> | <Literal>)

The same internal storage format, a DWORD, is used for the DATE and DT data types. The resolution for DATE is 1 day. The resolution for DT is 1 second. Both begin on January 1, 1970.

TOD is stored as a DWORD with a resolution of 1 millisecond.

Examples:

ST code

Result

DT_TO_DINT(DT#1970-1-1-0:0:0);

0

DATE_TO_DINT(D#1970-1-1);

0

TOD_TO_DINT(TOD#0:0:0);

0

DT_TO_DINT(DT#1970-1-1-0:0:1);

1

DATE_TO_DINT(D#1970-1-2);

86400

DT_TO_DINT(DT#2019-9-1-12:0:0.0);

1567339200

DATE_TO_DINT(D#2019-9-1);

1567339200

TOD_TO_DINT(TOD#12:0:0);

43200000

Converting date and time to STRING/WSTRING

Operators:

DATE_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
DT_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
TOD_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
LDATE_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
LDT_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
LTOD_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)

The operands of type DATE, DATE_AND_TIME, TIME_OF_DAY, DT or TOD, which are passed to an operator for a date and time conversion, are converted in their constant notation (literal notation). The generated string contains the keywords D#, DT# or TOD# and then the size with its date and time unit, as specified in the IEC 61131-3 standard.

Examples:

ST code

Result

TIME_TO_STRING(T#12ms);

'T#12ms'

DT_TO_STRING(DT#1998-02-13-14:20);

'DT#1998-02-13-14:20:00'

See also: