Date and Time Conversion

Hint

If the operand value for a type conversion operator is outside of the value range of the target data type, then the result output depends on the processor type and is therefore undefined. This is the case, for example, when a negative operand value is converted from LREAL to the target data type UINT.

Information can be lost when converting from larger data types to smaller data types.

The operators convert a date and time value into the specified data type and return a type-converted value.

Call syntax

<variable name> := <date and time conversion operator> ( <operand> );

<operand> = <variable name> | <literal>

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

Operators

DATE_TO___UXINT
DATE_TO___XINT
DATE_TO___XWORD
DATE_TO_BIT
DATE_TO_BOOL
DATE_TO_BYTE
DATE_TO_DINT
DATE_TO_DT
DATE_TO_DWORD
DATE_TO_INT
DATE_TO_LINT
DATE_TO_LREAL
DATE_TO_LTIME
DATE_TO_LWORD
DATE_TO_REAL
DATE_TO_SINT
DATE_TO_STRING
DATE_TO_TIME
DATE_TO_TOD
DATE_TO_UDINT
DATE_TO_UINT
DATE_TO_ULINT
DATE_TO_USINT
DATE_TO_WORD
DATE_TO_WSTRING

DT_TO___UXINT
DT_TO___XINT
DT_TO___XWORD
DT_TO_BIT
DT_TO_BOOL
DT_TO_BYTE
DT_TO_DATE
DT_TO_DINT
DT_TO_DWORD
DT_TO_INT
DT_TO_LINT
DT_TO_LREAL
DT_TO_LTIME
DT_TO_LWORD
DT_TO_REAL
DT_TO_SINT
DT_TO_STRING
DT_TO_TIME
DT_TO_TOD
DT_TO_UDINT
DT_TO_UINT
DT_TO_ULINT
DT_TO_USINT
DT_TO_WORD
DT_TO_WSTRING

TOD_TO___UXINT
TOD_TO___XINT
TOD_TO___XWORD
TOD_TO_BOOL
TOD_TO_BIT
TOD_TO_BYTE
TOD_TO_DATE
TOD_TO_DINT
TOD_TO_DT
TOD_TO_DWORD
TOD_TO_INT
TOD_TO_LINT
TOD_TO_LREAL
TOD_TO_LTIME
TOD_TO_LWORD
TOD_TO_REAL
TOD_TO_SINT
TOD_TO_STRING
TOD_TO_TIME
TOD_TO_UDINT
TOD_TO_UINT
TOD_TO_ULINT
TOD_TO_USINT
TOD_TO_WORD
TOD_TO_WSTRING

Long operators

LDATE_TO___UXINT
LDATE_TO___XINT
LDATE_TO___XWORD
LDATE_TO_BIT
LDATE_TO_BOOL
LDATE_TO_BYTE
LDATE_TO_DATE
LDATE_TO_DINT
LDATE_TO_DT
LDATE_TO_DWORD
LDATE_TO_INT
LDATE_TO_LDT
LDATE_TO_LINT
LDATE_TO_LREAL
LDATE_TO_LTIME
LDATE_TO_LTOD
LDATE_TO_LWORD
LDATE_TO_REAL
LDATE_TO_SINT
LDATE_TO_STRING
LDATE_TO_TIME
LDATE_TO_TOD
LDATE_TO_UDINT
LDATE_TO_UINT
LDATE_TO_ULINT
LDATE_TO_USINT
LDATE_TO_WORD
LDATE_TO_WSTRING

LDT_TO___UXINT
LDT_TO___XINT
LDT_TO___XWORD
LDT_TO_BIT
LDT_TO_BOOL
LDT_TO_BYTE
LDT_TO_DATE
LDT_TO_DINT
LDT_TO_DWORD
LDT_TO_INT
LDT_TO_LDATE
LDT_TO_LINT
LDT_TO_LREAL
LDT_TO_LTIME
LDT_TO_LTOD
LDT_TO_LWORD
LDT_TO_REAL
LDT_TO_SINT
LDT_TO_STRING
LDT_TO_TIME
LDT_TO_TOD
LDT_TO_UDINT
LDT_TO_UINT
LDT_TO_ULINT
LDT_TO_USINT
LDT_TO_WORD
LDT_TO_WSTRING

LTOD_TO___UXINT
LTOD_TO___XINT
LTOD_TO___XWORD
LTOD_TO_BOOL
LTOD_TO_BIT
LTOD_TO_BYTE
LTOD_TO_DATE
LTOD_TO_DINT
LTOD_TO_DT
LTOD_TO_DWORD
LTOD_TO_INT
LTOD_TO_LDATE
LTOD_TO_LDT
LTOD_TO_LINT
LTOD_TO_LREAL
LTOD_TO_LTIME
LTOD_TO_LWORD
LTOD_TO_REAL
LTOD_TO_SINT
LTOD_TO_STRING
LTOD_TO_TIME
LTOD_TO_UDINT
LTOD_TO_UINT
LTOD_TO_ULINT
LTOD_TO_USINT
LTOD_TO_WORD
LTOD_TO_WSTRING

Converting to a Boolean value

The operator returns FALSE if and only if the operand value can be interpreted as “0”.

xDate := DATE_TO_BOOL(D#1970-1-1); xDate = FALSE
xDateAndTime := DT_TO_BOOL(DT#1970-1-1-0:0:0); xDateAndTime = FALSE
xTimeOfDay := TOD_TO_BOOL(TOD#0:0:0); xTimeOfDay = FALSE
xDate := DATE_TO_BOOL(D#2019-9-1); xDate = TRUE
xDateAndTime := DT_TO_BOOL(DT#2019-9-1-12:0:0); xDateAndTime = TRUE
xTimeOfDay := TOD_TO_BOOL(TOD#12:0:0); xTimeOfDay = TRUE

Converting to an integer

The data types DATE and DT use the same memory format internally, namely a DWORD. The resolution for DATE is 1 day. The resolution for DT is 1 second. Both begin at January 1, 1970.

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

diReturn_0 := DT_TO_DINT(DT#1970-1-1-0:0:0); diReturn_0 = 0
diReturn_1 := DATE_TO_DINT(D#1970-1-1); diReturn_1 = 0
diReturn_2 := TOD_TO_DINT(TOD#0:0:0); diReturn_2 = 0
diReturn_1 := DT_TO_DINT(DT#1970-1-1-0:0:1); diReturn_3 = 1
diReturn_3 := DATE_TO_DINT(D#1970-1-2); diReturn_4 = 86400
diReturn_5 := DT_TO_DINT(DT#2019-9-1-12:0:0.0); diReturn_5 = 1567339200
diReturn_6 := DATE_TO_DINT(D#2019-9-1); diReturn_6 = 1567339200
diReturn_7 := TOD_TO_DINT(TOD#12:0:0); diReturn_7 = 43200000

Converting to a string

Hint

String manipulation when converting to STRING or WSTRING

When converting the type to STRING or WSTRING, the typed value is left-aligned as a character string and truncated if it is too long. Therefore, declare the return variable for the type conversion operators <>_TO_STRING and <>_TO_WSTRING long enough that the character string has enough space without any manipulation.

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

Examples

Examples

FBD implementation language

The controller is in online mode in order to monitor the variables.

See also