Access variable inside script

I am trying to access a Grafana variable carrying a timestamp in a script inside a text panel. Honestly I am not sure if this is even possible. This is the script where I would like to replace the timestamp string (“2023-07-27T12:05:18.788Z”) with a variable I have defined as “lastseen” which is constantly updated by the relevant query.

<div id="timeDiffContainer"></div>
var yourDateTimeVar = "2023-07-27T12:05:18.788Z";
var ..........

If I write the variable outside the script blockI can see the value in the text panel, but I need such value in the script :frowning:
How can I access the variable from inside the script?

Thank you for your answer.
It doesn’t wanna work!
lastseen is a variable I have defined in Grafana which query influx and contains a timestamp “2023-07-27T14:53:54.479Z”
To make sure the variable has a value, as you can see from the screenshot, it shows up in the text panel outside the script [[lastseen]] but looks like inside the script it doesn’t.


<div id="timeDiffContainer"></div>

var yourDateTimeVar = ${lastseen};
var dateTime = new Date(yourDateTimeVar);
var now = new Date();
var diff = now - dateTime;
var diffSeconds = Math.floor(diff / 1000);
var diffMinutes = Math.floor(diffSeconds / 60);
var diffHours = Math.floor(diffMinutes / 60);
var timeDiffContainer = document.getElementById('timeDiffContainer');
timeDiffContainer.innerHTML = "Last seen: " + diffHours + " hours, " + diffMinutes % 60 + " minutes, " + diffSeconds % 60 + " seconds ago.";

Does that Text plugin allow embedding javascript?

It’s not a plug-in. It is the standard text panel available in Grafana

does it allow inline javascript

Why you can’t use standard panel (= make it metric) and use unit From Now?

As I said in my post, I don’t know if such script would work in th text panel. I was trying in that way to start but I see it is going nowhere. I will have to change the approach. Thanks for helping

I am trying the “from Now” method. I am getting better results. I will try to refine this method. Thanks for the hint.