Convert time range to epoch time

Hey,

how can I convert time ranges like “&from=now-1M/M&to=now-1M/M” to epoch time?

Use Case: I execute JavaScript Code in the HTML plugin by aidanmountford. My code shall consider the selected time range. Currently, I take the “from” and “to” value from the browser URL. For absolute time ranges, I get already absolute epoch time but for the other options not. I was already pointed to rangeutil.ts but I don’t know if/how I can access this function from my JavaScript code.

Currently, I see no other way than re-implementing the complete date parsing on my own which is obviously not a good approach.

I’m running Grafana version v6.7.1.

I was already pointed to rangeutil.ts but I don’t know if/how I can access this function from my JavaScript code.

Not sure about 6.7, but in 7.x rangeUtil is available from @grafana/data package, so you can do smth like this:

import { rangeUtil } from '@grafana/data';

const {from, to} = rangeUtil.convertRawToRange({ from: 'now-30m', to: 'now' });
const epochFrom = from.valueOf();
const epochTo = to.valueOf();

With v6.7, the following works fine for me as mentioned in how-to-dynamic-get-system-time-range-variable:

let tr = angular.element('grafana-app').injector().get('timeSrv').timeRange();
let from = new Date(tr.from._d).toISOString();
let to = new Date(tr.to._d).toISOString();