# Formatting a date in an alert email

**URL:** <https://discuss.elastic.co/t/formatting-a-date-in-an-alert-email/332312>\
**Category:** Kibana\
**Tags:** elastic-stack-alerting\
**Created:** [May 2, 2023, 3:12pm UTC](https://discuss.elastic.co/t/formatting-a-date-in-an-alert-email/332312 "2023-05-02T15:12:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ppic](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@ppic](https://discuss.elastic.co/u/ppic)\
**Post date:** [May 2, 2023, 3:12pm UTC](https://discuss.elastic.co/t/formatting-a-date-in-an-alert-email/332312/1 "2023-05-02T15:12:15Z")

</div>

Hi,  
I use Kibana 7.17.  
In "Rules and Connectors", I created an alert generating an email.  
In this email, I display a date (value of a field) like : 2023-04-28T17:23:23.943Z  
The user would like a date like 28/04/2023 17 :23 :23  
Is there a way to do it (a kind of string filter) ?  
Thank you.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [May 3, 2023, 10:41am UTC](https://discuss.elastic.co/t/formatting-a-date-in-an-alert-email/332312/2 "2023-05-03T10:41:03Z")

</div>

Hi @ppic,

Where are you getting the date from? Is it the `{{date}}` value from the alert context?

---

<div class="post-metadata">

**Author:** ![ppic](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@ppic](https://discuss.elastic.co/u/ppic)\
**Post date:** [May 3, 2023, 12:14pm UTC](https://discuss.elastic.co/t/formatting-a-date-in-an-alert-email/332312/3 "2023-05-03T12:14:29Z")

</div>

Hi Carly,  
The date comes from a database.  
I display it in a loop like :

> {{#context.hits}} - Application : **{{\_source.sql.metrics.string.errmsg\_application}}** &nbsp; at : {{\_source.sql.metrics.string.errmsg\_date}}  
> {{/context.hits}}

(strange display if I use ``` instead of \>, it doesn't like #)  
That is the main point, but I am also interested in better readability of `{{context.date}}`.

Best rgds

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [May 3, 2023, 2:01pm UTC](https://discuss.elastic.co/t/formatting-a-date-in-an-alert-email/332312/4 "2023-05-03T14:01:03Z")

</div>

Hi @ppic,

Thanks for confirming. How are you ingesting your data from the DB into Elasticsearch? If you're using Logstash I would say you could use something like a [date filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-date.html). But I don't think it can be transformed in the message segment itself as that's just markdown.

---

<div class="post-metadata">

**Author:** ![ppic](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@ppic](https://discuss.elastic.co/u/ppic)\
**Post date:** [May 3, 2023, 3:42pm UTC](https://discuss.elastic.co/t/formatting-a-date-in-an-alert-email/332312/5 "2023-05-03T15:42:46Z")

</div>

Hi Carly,

I use the 'sql' module of 'metricbeat', without Logstash.  
I posted another item that is similar: the text of a db field in the alert mail has no more new lines (and then, becames hard to read).  
So, if there is no solution inside the mail connector, the only way is to transform the text upstream.

- In Kibana ?
- In the SQL query (in sql.yml) ? I am testing it....
- In the monitored application, and ask the dev team to produce text field dedicated to this purpose, taking into account the makdown syntax. That is heavy !  
The second point looks promising.  
I'll tell you soon...

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [May 4, 2023, 8:44am UTC](https://discuss.elastic.co/t/formatting-a-date-in-an-alert-email/332312/6 "2023-05-04T08:44:59Z")

</div>

Hi @ppic ,

I would probably go with either in the query itself as a first option. Give it a try and let me know. You could also make use of [Runtime fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html) as another option and translate it using Painless.

Let us know how you get on.

---

<div class="post-metadata">

**Author:** ![ppic](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@ppic](https://discuss.elastic.co/u/ppic)\
**Post date:** [May 4, 2023, 4:44pm UTC](https://discuss.elastic.co/t/formatting-a-date-in-an-alert-email/332312/7 "2023-05-04T16:44:43Z")

</div>

Hi Carly,

It works.  
I modified the SQL query with :

```auto
FORMAT(Date,'dd/MM/yyyy hh:mm:ss ') as 'ErrMsg_Date',

```

For the new lines (add two spaces before the NL) :

```auto
REPLACE( Message, CHAR(13) + CHAR(10), ' ' + CHAR(13) + CHAR(10) ) 'ErrMsg_Message' 

```

because the server runs Windows.  
I didn't understand the Runtime fields (I didn't spend time on it).  
Do you think it can transform {{context.date}} ?  
But by now, I'm satisfied with the SQL solution.  
Best rgds.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 1, 2023, 4:45pm UTC](https://discuss.elastic.co/t/formatting-a-date-in-an-alert-email/332312/8 "2023-06-01T16:45:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
