Skip to content

Date/Time Exercise

Exercise: Production Batch Timestamping

Please follow the exercise below to understand Date/Time functionality.

The following configuration is used to set up this exercise.

  1. Create the following tags:
    1. BatchStartTime
      • Data type: STRING
      • Initial value: (leave blank)
    2. BatchStarted
      • Data type: BOOL
      • Initial value: 0

  1. Create the following script:
    1. RecordBatchStart
      • Running type: Periodic (e.g., 1000 ms)

// Check if the batch has just been started
if (tag.read("BatchStarted") == 1) {
// Get current timestamp in milliseconds
var current = Date.now();
// Convert timestamp into a readable date string
var dateObj = new Date(current);
var formatted =
dateObj.getFullYear() + "/" +
("0" + (dateObj.getMonth() + 1)).slice(-2) + "/" +
("0" + dateObj.getDate()).slice(-2) + " " +
("0" + dateObj.getHours()).slice(-2) + ":" +
("0" + dateObj.getMinutes()).slice(-2) + ":" +
("0" + dateObj.getSeconds()).slice(-2);
// Write the formatted date/time to the BatchStartTime tag
tag.write("BatchStartTime", formatted);
// Reset trigger to prevent repeating
tag.write("BatchStarted", 0);
}

This script is used to capture the system time when a batch begins. The script uses built-in date commands available.

  1. Configure the project to match the layout shown in the image. It should include a date/time object, three text displays, and a Momentary Button.

  1. Label the text displays as "Current Time:" and "Batch Started At:", and rename the Momentary Button to "Start Batch".

  1. Drag the BatchStartTime tag onto the new text display in the second row.

  1. For the Momentary Button, go to On Press > Tag > Set Tag Value and assign the BatchStarted tag with a value of 1. This will trigger the script to capture the current time.

  1. Click Tools > Launch Simulator to start the Canvas Simulator.

  2. Press the Start Batch button.

  3. The BatchStartTime string will update with the exact timestamp, while the Date/Time object continues displaying the live system time.

  1. Each time the operator starts a new batch, the timestamp updates accordingly.