:::info Sample code
A working example is available on GitHub — [nodejs/plugin/timestamp](https://github.com/obniz/obniz/tree/master/examples/nodejs/plugin/timestamp).
:::

You can retrieve the elapsed time or attach timestamp information to data sent to obniz.js.

## cloud.enqueueTimestampByTick(tick)

By calling `os.getTick()`, you can determine how many milliseconds have elapsed since the obnizOS started.

Normally, when you send data using a method like `cloud.pluginSend("data");`, there is no way to know exactly when that data was generated unless you include the time within the data payload itself.

However, by using the `cloud.enqueueTimestampByTick(tick)` function before sending data, you can attach the specific time corresponding to the "tick" as a Unix timestamp (in milliseconds).

In the following example, two pieces of data are sent to the cloud every 3 seconds. The first piece is given a timestamp from 1 second ago, and the second piece is given a timestamp of the current time.

### Example Code (Lua)

```lua
tick = 0

function on_online_loop() 
  local current = os.getTick()
  if tick == 0 then
    tick = current
  end
  if current - tick >= 3 * 1000 then
    tick = current
    os.log(" - 3 seconds passed");

    -- Add old data with a specific timestamp (1 second ago)
    cloud.enqueueTimestampByTick(current - 1000);
    cloud.pluginSend("this is 1 sec old data");

    -- Add data with the current timestamp
    cloud.enqueueTimestampByTick(current);
    cloud.pluginSend("now");
  end
end

```

### Receiving Data on the JavaScript Side

On the JavaScript side, when data is received via `onreceive`, you can access the timestamp through `obniz.deviceTimestamp`.

This value represents the number of milliseconds elapsed since January 1, 1970 (Unix Epoch).

**Note**: Since the internal clock of the device may drift unless synchronized, it is necessary to call `obniz.setClock()` at the start and at regular intervals.

### Example Code (JavaScript)

```javascript
  // Synchronize the device clock
  obniz.setClock();

  obniz.plugin.onreceive = (data) => {
    // Calculate how old the data is by comparing the current time with deviceTimestamp
    const ageInSeconds = (Date.now() - obniz.deviceTimestamp) / 1000;
    console.log(`data "${Buffer.from(data).toString()}" is ${ageInSeconds} seconds old`);
  };

```