Reading System Time and Date into WinCC TIA Portal Tags
WinCC V7.x exposed system time and date through dedicated system channels that could be dragged directly onto a tag configuration. When projects migrated to WinCC V11 and later (TIA Portal, now at V18/V19), those system channels were removed from the tag editor. Engineers moving a working V7 project to TIA Portal therefore lose the "free" date/time tag and must reconstruct the functionality with a script, an area pointer, or a system diagnostic block. This article documents the field-proven pattern: declare an internal String or Date/Time tag, populate it from a scheduled VBScript (or C-Script), and route that tag into a UserArchive row for time-stamping production events.
1. Problem Definition
The original symptom reported in the field: a user inserts a clock object on a WinCC V11 screen in digital mode, assigns a tag to the Output value property, and the tag reads back as a blank value when read with GetTagChar or GetTagFloat. The clock face animates, but the value the script receives is empty.
Root cause: the clock object is a visual graphic primitive in the TIA Portal graphics designer. It is driven by the runtime's own date/time source; it does not push a value into a connected tag. Assigning a tag to the property only binds a reference for output of an input widget such as an I/O field, not the other way around. To capture the time, you must explicitly write it from script into a tag of compatible data type.
2. Prerequisites
- Siemens TIA Portal V11 SP2 or later (procedure verified through V17/V18; same code applies to V19).
- WinCC Comfort, Advanced, or Professional Runtime license on the HMI panel or PC.
- UserArchives option package installed and licensed if you intend to log the captured time.
- VB scripting or C-scripting enabled in the runtime settings: Runtime settings > Scripts > Allow VB scripts = Yes (Advanced/Professional only — Comfort panels do not support VBScript, use C-Script or a scheduled task with system functions).
- Project compiled and downloaded to the target device or RT simulation.
3. Tag Configuration in TIA Portal
3.1 Internal Tag Declaration
Open the HMI tag editor and create a new tag with the following properties:
| Property | Value | Notes |
|---|---|---|
| Name | TimeStamp_String |
Descriptive name; appears in script SmartTags() index. |
| Data type |
String (WString or String depending on panel) |
Length 24 characters minimum for "YYYY-MM-DD hh:mm:ss". |
| Connection | Internal tag | No PLC connection required. |
| Length | 24 | Adjust for locale-specific formats. |
| Update cycle | 1 s (or as required) | Must match the script trigger interval. |
For arithmetic processing (compare timestamps, compute duration) declare a second internal tag with data type DateTime. The DateTime type is stored as 8 bytes BCD and is supported on Comfort/Advanced panels and WinCC Professional.
3.2 Area Pointer Alternative (No Script)
If the requirement is only to display the time, configure the HMI area pointer Date/Time under Connections > Area pointers. The PLC can then write the controller time of day (S7-300/400/1500) to the panel. This is not the same as the runtime's local PC time and is not suitable when the time stamp must reflect the operator's local clock for audit purposes.
4. VBScript Solution
The recommended pattern uses a scheduled VBScript that calls the VBScript Time(), Date(), and Now() intrinsic functions and writes the result to the internal String tag. Schedule the script with a 1-second trigger.
4.1 VBScript Source
' VBScript: write system time and date to internal String tags
' Trigger: 1 s cyclic, event-driven, or on a button click
Dim sTime, sDate, sNow
sTime = Time() ' returns "hh:mm:ss"
sDate = Date() ' returns "mm/dd/yyyy" (US locale)
sNow = Now() ' returns "mm/dd/yyyy hh:mm:ss"
' Write to internal string tags
SmartTags("TimeStamp_Time").Value = CStr(sTime)
SmartTags("TimeStamp_Date").Value = CStr(sDate)
SmartTags("TimeStamp_Now").Value = CStr(sNow)
' Optional: parse to Date/Time tag for arithmetic
SmartTags("TimeStamp_DT").Value = CDate(sNow)
' Optional: ISO-8601 format for sorting and database logging
SmartTags("TimeStamp_ISO").Value = Year(Now) & "-" & _
Right("0" & Month(Now), 2) & "-" & _
Right("0" & Day(Now), 2) & " " & _
Right("0" & Hour(Now), 2) & ":" & _
Right("0" & Minute(Now), 2) & ":" & _
Right("0" & Second(Now), 2)
' Trace output to diagnostics window
HMIRuntime.Trace "Time: " & sNow & vbNewLine
4.2 Trigger Configuration
- Open HMI tags > [your tag] > Events or create a new VB function under Project tree > Scripts > VB scripts.
- Right-click the function > Properties > Triggers > add a new trigger.
- Set trigger type to Cyclic with cycle
1 s(or 500 ms for smoother display). - Compile the project and download. Verify the apdiag trace window shows the time string updating every second.
Date() returns the format dictated by the regional settings of the runtime device. If the HMI is later redeployed to a region with different locale, the date format shifts. Use the explicit ISO-8601 construction in the example above for any value destined for a database or UserArchive column that is queried by date comparison.5. C-Script Equivalent
Engineers constrained to C-Scripting (Comfort panels, or legacy code bases) can use the standard ANSI C runtime functions. The tag read/write functions are still GetTagChar, SetTagChar, GetTagFloat, SetTagFloat depending on tag type.
5.1 C-Script Source
/* C-Script: write system time and date to internal tags */
#include <time.h>
void WriteTime(void)
{
time_t now;
struct tm* t;
char buf[32];
time(&now);
t = localtime(&now);
/* Format 1: ISO-8601 with seconds */
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", t);
SetTagChar("TimeStamp_ISO", buf);
/* Format 2: just the time */
strftime(buf, sizeof(buf), "%H:%M:%S", t);
SetTagChar("TimeStamp_Time", buf);
/* Format 3: just the date */
strftime(buf, sizeof(buf), "%d.%m.%Y", t);
SetTagChar("TimeStamp_Date", buf);
printf("Time written: %s\n", buf);
}
The reason the original poster saw a blank value with GetTagChar is that the WinCC runtime returns a zero-length string from a tag that was never written to, or returns the tag's declared default (which is empty for a newly created internal String tag). Adding the script as a scheduled event resolves the read-back.
6. Routing the Timestamp into a UserArchive
6.1 UserArchive Column Definition
In the UserArchive configuration, define the time/date column with these properties:
| Property | Recommended Value | Reason |
|---|---|---|
| Name | EventTime |
Descriptive. |
| Data type |
String (24) or DateTime
|
String gives format flexibility; DateTime allows sorting. |
| Length | 24 | Fits ISO-8601 string with seconds. |
| Index | Enabled if used in WHERE clauses | Improves query performance on large archives. |
6.2 Inserting a Row with the Timestamp
Use the VBScript HMIRuntime.BaseHMI UserArchive object model. The standard pattern is:
' VBScript: insert a new archive row with current timestamp
Dim ua, row, ret
Set ua = HMIRuntime.BaseHMI.UserArchive("ProductionEvents")
' Build a new row
Set row = ua.Add()
' Populate fields
row.Item("EventTime").Value = SmartTags("TimeStamp_ISO").Value
row.Item("Operator").Value = SmartTags("CurrentOperator").Value
row.Item("PartNumber").Value = SmartTags("PartNumber").Value
row.Item("Result").Value = SmartTags("Result").Value
' Commit to the archive (writes to disk and ring buffer)
ret = row.Write()
If ret <> 0 Then
HMIRuntime.Trace "UserArchive write failed: " & ret & vbNewLine
End If
' Optional: clean up the row object
Set row = Nothing
Set ua = Nothing
Trigger this script from the same button that logs the production event. The 1-second cyclic refresh of TimeStamp_ISO guarantees that the value written into the archive is no more than one second stale at the moment the button is pressed.
7. Scheduled Event vs. Button-Triggered Event
There are three common trigger points for the timestamp script:
| Trigger | Configuration | Best For | Cost |
|---|---|---|---|
| 1 s cyclic on tag | Tag > Events > Value change with cycle | Continuous display + on-demand logging | Always runs in background; negligible CPU on PC, ~0.5 % on Comfort panels. |
| On button click | Button > Events > Click > VBScript | Event logging only (no live clock display) | No background load; tag is updated only on event. |
| On screen change | Screen > Events > Loaded | Per-screen timestamp; useful for "screen last opened at" audit | Refreshed only on navigation. |
8. Display on a Screen
Drop an I/O field on the screen, set Mode to Output, set the Tag to TimeStamp_ISO, and configure the Format property to yyyy-MM-dd HH:mm:ss. The field will display whatever the script most recently wrote.
For the date/time to update visually without page refresh, the underlying tag must have its Update cycle set, and the I/O field property Refresh set to Visible. The WinCC digital clock object already handles this; pairing it with the I/O field driven by the script is the standard solution.
9. Troubleshooting Matrix
| Symptom | Likely Cause | Resolution |
|---|---|---|
GetTagChar returns blank string |
Tag never written; no script trigger attached | Add cyclic trigger; confirm Trace window shows the write |
| Tag value updates but I/O field shows "####" | Field length shorter than tag string | Increase field Length to 24 or more |
| VBScript error "Object required: SmartTags(...)" | Tag name misspelled or tag deleted after compile | Re-compile project; verify spelling matches the tag editor |
| Date displays in MM/DD format on a German panel | Locale-dependent Date()
|
Replace with explicit ISO-8601 construction |
UserArchive row.Write() returns non-zero |
Archive not opened, or path unreachable | Check HMI Runtime > UserArchive status; verify the .csv path is writable on the panel/PC |
| Time drifts by several minutes per day | Panel battery dead; PC time not synced | Replace buffer battery; configure NTP sync on the runtime PC |
| Script fires but tag remains empty on Comfort panel | Comfort does not support VBScript | Use C-Script variant, or upgrade to Advanced/Professional license |
| Clock shows local time but archive requires UTC | Default Time() is local |
Use VBScript DateAdd("h", -TimeZoneBias, Now) or C gmtime()
|
10. Synchronization with PLC Time
If the PLC is the master clock (typical in S7-1500 projects with T_CONFIG time synchronization), the HMI should display the controller time, not the panel local time. Configure an area pointer of type Date and Time from the PLC to the HMI, and the HMI displays the S7-1500 time-of-day. For logging in a UserArchive on the HMI side, read the same S7 clock with:
' VBScript: read PLC time from area pointer tag
Dim sPlcTime
sPlcTime = SmartTags("PLC_Date_And_Time").Value
SmartTags("TimeStamp_ISO").Value = sPlcTime
The PLC tag must be of type DTL (S7-1500) or DT (S7-300/400) and is automatically populated by the area pointer configuration under Connections > Area pointers > Date and Time.
11. Performance and Memory Considerations
Writing one String tag of length 24 bytes once per second consumes a negligible share of the runtime's tag update bandwidth. The trace output to the diagnostics window, however, is more expensive: HMIRuntime.Trace writes to a ring-buffered log file, and on a Comfort panel the flash memory has a finite write endurance. Recommendations:
- Enable trace only during commissioning; disable for production release.
- Avoid writing the timestamp string more frequently than the screen update rate (typically 1 Hz for analog clock visuals).
- For a UserArchive that records every second, configure the archive's Data record length and ring buffer to match the retention requirement (e.g. 86 400 rows for 24 h at 1 Hz).
12. Verifying the Implementation
- Compile and download the project to the target HMI or RT PC.
- Start runtime; open the diagnostics window (Start > SIMATIC > WinCC > Diagnostics).
- Confirm
Traceshows the timestamp string updating once per second. - Insert a test row into the UserArchive via the VBScript button; export the archive to CSV and confirm the
EventTimecolumn contains ISO-8601 strings in the expected sequence. - Force a runtime restart; verify the cyclic script resumes and the tag value repopulates within one cycle (≤ 1 s).
- Disconnect network and reconnect; verify the script continues to run (no dependency on the PLC connection for the local-time case).
Why does my WinCC TIA Portal tag read back as blank when I assign it to a clock object?
The clock object is a visual graphic primitive that displays the runtime's local time; it does not output the value to a connected tag. To capture the time into a tag, use a scheduled VBScript or C-Script that calls Time(), Date(), or Now() and writes the result with SmartTags(...).Value = ... (VBScript) or SetTagChar (C-Script).
Can I capture the time into a tag without writing a script?
Yes, if your PLC is the master clock. Configure the Date and Time area pointer under Connections > Area pointers; the PLC's S7-1500 DTL or S7-300/400 DT value will be written to a designated HMI tag automatically. This is the only no-script method and reflects PLC time, not the panel's local clock.
Which data type should the timestamp tag be?
Use String with length ≥ 24 for display and for writing into a UserArchive String column. Use DateTime if you need to perform arithmetic (duration, comparison, sorting) — it stores 8 bytes BCD and is supported on Comfort/Advanced panels and WinCC Professional.
How do I write the timestamp into a UserArchive row?
Use the VBScript object model: HMIRuntime.BaseHMI.UserArchive("ArchiveName").Add() to build a new row, set the EventTime field to the timestamp string, and call row.Write(). A non-zero return indicates a write error; check archive path and field definitions.
Why does my date format change when the HMI is redeployed to a different region?
VBScript's Date() and Now() return locale-dependent strings. Build the timestamp explicitly with Year(), Month(), Day(), Hour(), Minute(), Second() and zero-pad the result, or use C-Script's strftime("%Y-%m-%d %H:%M:%S"), to produce a locale-independent ISO-8601 string suitable for database logging.