Get_CPU_DateTime

Overview

The Get_CPU_DateTime function block retrieves the current date and time from the PLC CPU real-time clock (RTC). The returned values are provided as separate fields for month, day, year, hours, minutes, and seconds.

Month           =>  (USINT)
Day             =>  (USINT)
Year            =>  (USINT)
Hours           =>  (USINT)
Minutes         =>  (USINT)
Seconds         =>  (USINT)

Interface

Inputs

This function block has no inputs.

💡 RTC Source

The values returned by this function reflect the CPU real-time clock (RTC). The RTC is set at program download, using the PC’s date and time. It continues to run independently of the PLC program execution, and is maintained by a backup battery when the PLC is powered off.

Outputs

NameTypeRange / UnitsDescription
MonthUSINT1 - 12The current month.
DayUSINT1 - 31The current day of the month.
YearUSINT0 - 255The current year.
HoursUSINT0 - 23The current hour.
MinutesUSINT0 - 59The current minute.
SecondsUSINT0 - 59The current second.

Example:

PROGRAM PROG_Get_CPU_DateTime
    VAR
      CPU_Time   : Get_CPU_DateTime;
      Month      : USINT;
      Day        : USINT;
      Year       : USINT;
      Hours      : USINT;
      Minutes    : USINT;
      Seconds    : USINT;
    END_VAR

    (* Read CPU date/time *)
    CPU_Time();

    (* Copy outputs to local variables (optional) *)
    Month   := CPU_Time.Month;
    Day     := CPU_Time.Day;
    Year    := CPU_Time.Year;
    Hours   := CPU_Time.Hours;
    Minutes := CPU_Time.Minutes;
    Seconds := CPU_Time.Seconds;
END_PROGRAM


CONFIGURATION Config0
    RESOURCE Res0 ON PLC
        TASK task1_100ms(INTERVAL := T#100ms, PRIORITY := 1);
        PROGRAM Prog_Instance_GetCPUDateTime WITH task1_100ms : PROG_Get_CPU_DateTime;
    END_RESOURCE
END_CONFIGURATION