Dashboards & Visualizations

How to get time picker earliest and latest in epoch with JavaScript?

fredclown
Builder

I have a simple xml dashboard that I am doing some custom JavaScript with. I would like to get the earliest and latest from the time picker. However, if the time picker is set to today I am getting "@d" for the earliest and "now" for the latest. Are there any helper functions to convert relative time to epoch? You can see this in my simplified code example below.

 

 

...
var defaultTokens = mvc.Components.get("default");

var earliest = defaultTokens.get('timePicker.earliest'); //when time picker is today this returns @d
var latest = defaultTokens.get('timePicker.latest'); //when time picker is today this returns now
...

 

 

 

Labels (4)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@fredclown 

I think there is no simple way to get epoch values from the timerange picker but I have another calculated way.

You can execute the below search and get epoch time. 

| makeresults | addinfo | table info_min_time info_max_time

 

There are two ways to execute this search.

  1.  from dashboard XML
  2. from Javascript 

Check the below link for the first way.

https://community.splunk.com/t5/Splunk-Search/Converting-relative-time-into-epoch-for-the-time-range...

You will have the epoch time in the `tokEarliest` and `tokLatest` tokens. Just need to access it from Javascript.

 

Check the below solution for the second way.

 

require([
        'underscore',
        'jquery',
        'splunkjs/mvc',
        'splunkjs/mvc/simplexml/ready!'
      ], function(_, $, mvc) {
        console.log("taskCollectionTable");
              
              var earliestEpoch;
              var latestEpoch;
      
      
              var SearchManager = require("splunkjs/mvc/searchmanager");
              // Create the search manager
              var mysearch =  new SearchManager({
                      // id: "search1",
                      app: "search",
                      earliest_time: "$timePicker.earliest$",
                      latest_time: "$timePicker.latest$",
                      search: "| makeresults | addinfo | table info_min_time info_max_time"
              }, {tokens: true});
      
              mysearch.on('search:done', function(properties) {
                      var myResults = mysearch.data("results");
                      myResults.on("data", function () { 
                              resultArray = myResults.data().rows;
                              earliestEpoch = resultArray[0][0];
                              latestEpoch = resultArray[0][0];
      
                              console.log("earliestEpoch", earliestEpoch);
                              console.log("latestEpoch", latestEpoch);
                      });
      
              });

});

 

I hope this will help you.

 

Thanks
KV
If any of my replies help you to solve the problem Or gain knowledge, an upvote would be appreciated.

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...