How to dynamic get system time range variable?

Is it possible to get system time range variable?
I want to retrieve the variable and then place in my dashboard.

Please give me some suggestions.
Thank you.

There is no time range variable. How do you want to use it?

I want to retrieve it and then push into my dashboard.

Example:
Last 2 days
Last 7 days

Is it possible to get the information ?
Thanks for your great support.

no there is no way to do that.

Do you have any suggestions about it?
Or maybe I can retrieve the raw data and then push into it.

Thank you!

Approach like this

function getDashboardTimeRange(){
    var tr = angular.element('grafana-app').injector().get('timeSrv').timeRange();
    return ['Current dashboard time range: from <', tr.from._d, '> to <', tr.to._d, '>'].join('');
}
// add this to some element on dashboard.
$('#some_id).append(getDashboardTimeRange());

allows to get dashboard time range. One can use it in in Text panel to append/modify any content of dashboard.
The question here is how to insure that this custom javascript is always run on dashboard load, even when Text panel is collapsed…

Hi Yuyu,

I am a beginner for Grafana,Could you please tell me how should I use this code and place where to retrieve the time range variable data?
Thanks for your great support in advance.

It depends on what you want to achieve.

If the goal is to add time range info somewhere in dashboard:

  1. Add “Text” panel and use “html” mode in panel options editor
  2. Put your html markup like this in panel Content
<div id="time_range_info">
Custom dashboard time range info returned by function will be added here.<br>
</div>
<script>
function getDashboardTimeRange(){
   var tr = angular.element('grafana-app').injector().get('timeSrv').timeRange();
   return ['Current dashboard time range: from &lt;', tr.from._d, '&gt; to &lt;', tr.to._d, '&gt;'].join('');
}
$('#time_range_info').append(getDashboardTimeRange());
</script>

Some basic html, javascript and DOM manipulationknowledge is needed to make this approach really useful.
Note, that it won’t give you time range as variable to be used like template variables in panels.

Hi Yuyu,

My goal is to get time range variable to put into the dashboard.

For example:

If I choose Last 2 days then dashboard will show Last 2 days information in dashboard.
If I choose Last 7 days then dashboard will show Last 7 days information in dashboard.

If it possible to do it in Grafana or I just need to do it in Kibana?

Thanks for your great support.
Thanks a lot.

Wondering if anything came of this.

@cindy did you manage to make this feature ?

I am running version 7.0.6 and I can’t get the html code working, it just shows the html code from the script on my dashboard. Does someone know if this work around still works or another solution?

Found the reason, needed to set “disable_sanitize_html = true” in grafana.ini

I modified the script a little bit because the “from” and “to” are available in variable. They are in ms.

<div id="time_range_info">
Time window: 
</div>

<script>
  function getDashboardTimeRange(){
    var f = (new Date($__from) + '').split(' ');
    fWeekDay = f[0];
    fMonth = f[1];
    fDay = [f[2], ','].join('');t
    fYear = f[3];
    var from = [fWeekDay, fMonth, fDay, fYear].join(' ');
  
    var t = (new Date($__to) + '').split(' ');
    tWeekDay = t[0];
    tMonth = t[1];
    tDay = [t[2], ','].join('');
    tYear = t[3];
    var to = [tWeekDay, tMonth, tDay, tYear].join(' ');

    return ['from', from, ' to ', to].join(' ');

//    if you only need month and year for instance  
//    return [tMonth, tYear].join(' ');
  }
  $('#time_range_info').append(getDashboardTimeRange());
</script>
indent preformatted text by 4 spaces