Date and time
TapHome script date and time functions — NOW, DATETIME, DATETIMEADD, TIMESPAN and LOCALTIMEZONE — and the properties of datetime and timespan values.
Date and time functions create datetime values (a point in time) and timespan values (a duration). They work in every script — smart rules, notifications, Modbus and Packet parser — and use the time zone of the control unit. All functions on this page need Core 2022.2 or newer.
NOW
NOW returns the current date and time of the control unit as a datetime value — local time by default, UTC with NOW(Utc).
| |
| Name | Type | Description |
|---|---|---|
kind | DateTimeKind, optional | Local (default) or Utc. NOW(DateTimeKind.Utc) is the same as NOW(Utc). |
Remarks — A property cannot be read directly from a function call: NOW().HOUR is rejected when you save the script. Store the value in a variable first.
Example
VAR now := NOW();
now.HOUR;
# Result: the current hour, 0 to 23See also — DATETIME, Properties of datetime values.
DATETIME
DATETIME creates a datetime value from its parts, from text or from a number of milliseconds; called without arguments it returns 1.1.0001 00:00, not the current time — use NOW for that.
| |
| Name | Type | Description |
|---|---|---|
year, month, day | number | Required together. Omitted time parts are 0. |
hour … millisecond | number, optional | Time of day. |
kind | DateTimeKind, optional | Local (default) or Utc. |
text | string | Date and time as text. |
format | string, optional | .NET format of text, for example "dd.MM.yyyy HH:mm". |
milliseconds | number | Milliseconds since 1.1.0001 — the scale of the TICKS property, not Unix time. |
Remarks
- Text without a format is read according to the language settings of the control unit. Use ISO 8601 (
"2026-01-15T08:30:00") or pass a format so the result does not depend on them. - Text with an offset or
Z("2026-01-15T08:30:00Z") is kept as a UTC value. Withkindset toUtc, text without zone information is read as UTC. - For Unix time, set the UNIXTIME property.
Example
VAR start := DATETIME(2026, 1, 15, 8, 30, 0);
VAR iso := DATETIME("2026-01-15T08:30:00");
VAR custom := DATETIME("15.01.2026 08:30", "dd.MM.yyyy HH:mm");
VAR utc := DATETIME(2026, 1, 15, 8, 30, 0, 0, DateTimeKind.Utc);
custom.MINUTE;
# Result: 30DATETIMEADD
DATETIMEADD returns a new datetime moved by the given number of years, months, days, hours, minutes, seconds and milliseconds; negative numbers move it back.
| |
Remarks — The original value does not change. Adding a month to 31 January gives the last day of February. To add an interval, you can also use + with a timespan or with a number of milliseconds.
Example
VAR now := NOW();
VAR tomorrow := DATETIMEADD(now, 0, 0, 1);
VAR twoHoursAgo := DATETIMEADD(now, 0, 0, 0, -2);
VAR endOfFebruary := DATETIMEADD(DATETIME(2026, 1, 31), 0, 1);
endOfFebruary.DAY;
# Result: 28TIMESPAN
TIMESPAN creates a timespan value — a duration — either from milliseconds or from days, hours, minutes, seconds and milliseconds.
| |
Properties (read-only)
| Property | Meaning |
|---|---|
DAYS, HOURS, MINUTES, SECONDS, MILLISECONDS | One component of the duration (HOURS is 0 – 23). |
TOTALDAYS, TOTALHOURS, TOTALMINUTES, TOTALSECONDS, TOTALMILLISECONDS | The whole duration in that unit, with a fraction. |
Remarks
- Only the 1- and 5-argument forms exist; 2 to 4 arguments stop the script.
timespan ± timespangives atimespan,datetime ± timespanadatetime.- Subtracting two
datetimevalues is not defined. For the time between two dates, subtract theirTICKS(milliseconds). - Declaring a variable with the
timespantype keyword needs Core 2023.2 or newer.
Example
VAR t := TIMESPAN(1, 2, 3, 4, 0);
t.TOTALSECONDS;
# Result: 93784VAR start := DATETIME(2026, 1, 15, 8, 0, 0);
VAR stop := DATETIME(2026, 1, 15, 9, 30, 0);
(stop.TICKS - start.TICKS) / 60000;
# Result: 90 (minutes)Errors — TIMESPAN Unsupported number of parameters; Operator ‘-’ not defined for ‘DateTime’ and ‘DateTime’.
LOCALTIMEZONE
LOCALTIMEZONE returns the current offset of the control unit’s time zone from UTC in milliseconds, daylight saving time included.
| |
Example
LOCALTIMEZONE() / 3600000;
# Result: 1 in Central European winter time, 2 in summer timeProperties of datetime values
A datetime value has these properties. Read them from a variable, not from a function call.
| Property | Read | Write | Note |
|---|---|---|---|
TICKS | ✓ | ✓ | Milliseconds since 1.1.0001. |
YEAR, MONTH, DAY | ✓ | ✓ | Writing checks the range (MONTH 1 – 12, DAY 1 – last day of the month). |
HOUR, MINUTE, SECOND, MILLISECOND | ✓ | ✓ | Ranges 0 – 23, 0 – 59, 0 – 59, 0 – 999. |
DAYOFWEEK | ✓ | DayOfWeek value (Monday … Sunday). | |
DAYOFYEAR | ✓ | 1 – 366. | |
KIND | ✓ | ✓ | DateTimeKind: Local, Utc or Unspecified. |
UTCDATETIME, LOCALDATETIME | ✓ | The same moment converted with the time zone of the control unit. | |
UNIXTIME | ✓ | ✓ | Seconds since 1.1.1970 UTC. Writing it stores a UTC value. |
Remarks
- Outside a function argument an enum value is written with its type:
DayOfWeek.Saturday. A bareSaturdayis an unknown variable. <,>and=compare twodatetimevalues as moments in time, so values of differentKINDcompare correctly.- Writing a value outside the range stops the script, for example Value must between 0-23.
- Without a format, TOSTRING prints a
datetimeasyyyy/MM/dd hh:mm:ss.fff— a 12-hour clock without AM or PM. Pass a format such as"yyyy-MM-dd HH:mm".
Example — no action at the weekend
VAR now := NOW();
IF now.DAYOFWEEK = DayOfWeek.Saturday OR now.DAYOFWEEK = DayOfWeek.Sunday
RETURN(0);
END
1;Example — date from a Unix timestamp
datetime d;
d.UNIXTIME := 1767225600;
d.YEAR;
# Result: 2026 (1 January 2026, 00:00 UTC)See also — Format strings, Data types.