Date and Time
Temporal Logic
Master the clock of your PLC. From precise event timestamping to complex time calculations and format conversions, control every millisecond.
Why manage time in a PLC?
Time management is crucial for industrial automation. It allows for precise logging of production events, triggering scheduled maintenance tasks, calculating machine cycle times, and synchronizing distributed systems via NTP protocols.
Modern Siemens PLCs offer advanced data types like DTL (Date and Time Long) which provides direct access to individual components (Year, Month, Day, Hour) without complex bit manipulation, greatly simplifying programming compared to legacy DT formats.
Reading Time
Accessing the system and local clocks
T_CONV — System Time Extraction
T_CONVWhat it does
Extracts specific components from a date/time value or converts between formats. It is essential for obtaining readable hours/minutes from a raw timestamp.
When to use it
When you need to display only the time on an HMI from a complete Date_And_Time tag.
Pro Tips
Use T_CONV to convert a TOD (Time_Of_Day) to a String for easy logging.
Ensure the input and output types are compatible to avoid overflow errors.
In SCL, this instruction is often implicitly handled by simple assignments.
RD_SYS_T — Read System Time
RD_SYS_TWhat it does
Reads the current system time of the CPU (UTC). Unlike RD_LOC_T, it does not include daylight saving time or time zone offsets.
When to use it
Ideal for internal logging where a continuous, non-jumping time reference is required (avoids issues during DST changes).
Pro Tips
Always use UTC for database storage to ensure global consistency.
The RET_VAL output provides status information (e.g., if the clock is synchronized).
Combine with RD_LOC_T if you need to display both 'Technical' and 'Human' time.
Arithmetic
Calculations on durations and timestamps
T_ADD — Add Time
T_ADDWhat it does
Adds a duration (Time or LTime) to a specific date/time point. It correctly handles overflows of minutes, hours, and days.
When to use it
Calculating an expiration time or a scheduled restart point (e.g., current time + 24 hours).
Pro Tips
Very useful for implementing custom watchdogs with dynamic durations.
Use LTime types for nanosecond precision on S7-1500 CPUs.
If the result exceeds the maximum date (Year 2262), the CPU sets an error status.
T_DIFF — Time Difference
T_DIFFWhat it does
Calculates the difference between two timestamps and returns the result as a duration (Time or LTime).
When to use it
Measuring the exact duration of a process step or calculating the uptime of a machine.
Pro Tips
Perfect for OEE (Overall Equipment Effectiveness) calculations.
Subtracting a larger date from a smaller one returns a negative Time value.
On S7-1200/1500, use DTL tags for easier access to the difference components.
Conversion
Format changes and String handling
T_CONV — DT to DTL Conversion
T_CONVWhat it does
Converts legacy DATE_AND_TIME (8 bytes) to the modern DTL format or vice-versa.
When to use it
When interfacing legacy code or communication blocks with new S7-1500 optimized logic.
Pro Tips
Prefer DTL for all new projects to avoid BCD (Binary Coded Decimal) complexity.
Conversion to DTL allows you to write 'MyTag.MONTH' directly in your code.
Be careful with millisecond precision which might be truncated in some formats.
DT_TO_STRING — Serializing Time
DT_TO_STRWhat it does
Converts a date/time value into a readable String format. STRING_TO_DT does the inverse.
When to use it
Sending timestamps to a CSV file on a memory card or communicating with a web API.
Pro Tips
The output string follows the ISO 8601 format (YYYY-MM-DD-HH:MM:SS.ms).
Use STRING_TO_DT to parse dates received from an external SQL database.
Note that String operations are more CPU intensive; use them sparingly in fast loops.
DATE_AND_TIME vs DTL
Choosing the right temporal structure
| Feature | DATE_AND_TIME (Legacy) | DTL (Modern) |
|---|---|---|
| Size | 8 Bytes | 12 Bytes |
| Encoding | BCD (Complex to read) | Integers (Directly readable) |
| Component Access | Requires bit masking | Structural (Tag.Month) |
| Range | 1990 to 2089 | 1970 to 2262 |
| S7-1200/1500 Support | Compatibility only | Native & Recommended |
| Precision | 10 ms | 1 Nanosecond |
Frequently Asked Questions
What is the difference between System Time and Local Time?
System Time (RD_SYS_T) is always UTC, providing a stable reference. Local Time (RD_LOC_T) adjusts for your specific time zone and Daylight Saving Time (DST) rules configured in the CPU properties.
How to synchronize PLC clock with a server?
Enable NTP (Network Time Protocol) in the CPU Hardware Configuration. Provide the IP of your time server, and the PLC will automatically adjust its internal clock.
Why does my DATE_AND_TIME show weird values in Hex?
Legacy DT uses BCD encoding. A value of 25 in Decimal is stored as 16#25 in BCD. Always use conversion instructions or DTL to read values correctly.
Can I calculate the difference between two DTL tags directly?
In SCL, you can simply use the '-' operator between two DTL tags. TIA Portal will automatically call the T_DIFF instruction for you.