# Getting from..to datetime range in alert message template

**URL:** <https://community.grafana.com/t/getting-from-to-datetime-range-in-alert-message-template/74013>\
**Category:** Alerting\
**Tags:** templating\
**Created:** [October 12, 2022, 8:33am UTC](https://community.grafana.com/t/getting-from-to-datetime-range-in-alert-message-template/74013 "2022-10-12T08:33:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bensnv](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@bensnv](https://community.grafana.com/u/bensnv)\
**Post date:** [October 12, 2022, 8:33am UTC](https://community.grafana.com/t/getting-from-to-datetime-range-in-alert-message-template/74013/1 "2022-10-12T08:33:48Z")

</div>

Hello, I am trying to create a message template that on alert in Slack, will allow the user to click and see the dashboard focused on the relevant datetime range. However it seems like I do not have time.ParseDuration function available to create a Duration to [add to the Time](https://pkg.go.dev/time#Time.Add), I can only convert the [template data’s StartsAt](https://grafana.com/docs/grafana/latest/alerting/contact-points/message-templating/template-data/) to a unixMilli

my current template

```auto
{{ if gt (len .DashboardURL ) 0 }}Go to dashboard: {{ .DashboardURL }}/engine-backend?from={{ print .StartsAt.UnixMilli.Add (time.ParseDuration "-1h") }}&to={{ print .StartsAt.UnixMilli }}{{ end }}

```

I’ve only started working with Go templates for this, have no previous experience with Go.  
Does anyone have any idea how to do this?

---

<div class="post-metadata">

**Author:** ![bensnv](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@bensnv](https://community.grafana.com/u/bensnv)\
**Post date:** [October 14, 2022, 3:29am UTC](https://community.grafana.com/t/getting-from-to-datetime-range-in-alert-message-template/74013/2 "2022-10-14T03:29:44Z")

</div>

Found a solution, just need time.window variable as described in [Manage dashboard links | Grafana documentation](https://grafana.com/docs/grafana/v9.0/dashboards/manage-dashboard-links/#dashboard-links)

---

<div class="post-metadata">

**Author:** ![pepecano](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/pepecano/32/40676_2.png) [@pepecano](https://community.grafana.com/u/pepecano)\
**Post date:** [June 3, 2025, 8:38pm UTC](https://community.grafana.com/t/getting-from-to-datetime-range-in-alert-message-template/74013/3 "2025-06-03T20:38:32Z")

</div>

Hi there! 👋

Here’s an [example from the docs](https://grafana.com/docs/grafana/latest/alerting/configure-notifications/template-notifications/examples/#print-a-link-to-a-dashboard-with-time-range) that shows two ways to link an alert rule to a dashboard with a time range:

1. **Grafana 12 and later** – Use the built-in `DashboardURL` and `PanelURL` annotations.
2. **Custom annotation** – Build a time range manually in your template. For example:

```auto
  {{- $from := (.StartsAt.Add -3600000000000).UnixMilli }}

  {{- $to := "" }}
  {{- if eq .Status "resolved" }}
     {{- $to = (.EndsAt).UnixMilli }}
  {{- else -}}
    {{/* Use current time if alert is firing */}}
    {{- $to = (time.Now).UnixMilli }}
  {{- end -}}

  Dashboard: {{.Annotations.MyDashboardURL}}?from={{$from}}&to={{$to}}

```

This approach uses the alert’s start and end times to create a dynamic time range in the link. Hope this helps!
