# How can I format a seconds value in a table?

**URL:** https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576
**Category:** Kibana
**Created:** [May 14, 2020, 8:03am UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576 "2020-05-14T08:03:58Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Hugo\_Pires](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugo_pires/32/68069_2.png) [@Hugo\_Pires](https://discuss.elastic.co/u/Hugo_Pires)
#### Post date: [May 14, 2020, 8:03am UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/1 "2020-05-14T08:03:58Z")

</div>

Hello

I have a value in seconds that I want to show in a more readable format like HH:MM:SS.

How can I do it?

Thank you

---

<div class="post-metadata">

### Author: ![tiagocosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiagocosta/32/40045_2.png) [@tiagocosta](https://discuss.elastic.co/u/tiagocosta)
#### Post date: [May 19, 2020, 2:57pm UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/2 "2020-05-19T14:57:45Z")

</div>

Hi,

I think you can do this by either using a data formatter ([https://www.elastic.co/guide/en/kibana/current/managing-fields.html](https://www.elastic.co/guide/en/kibana/current/managing-fields.html)) or by creating a scripted field ([https://www.elastic.co/guide/en/kibana/current/scripted-fields.html](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html)) with something like

```auto
def dateFormat = new SimpleDateFormat("HH:mm:ss");
return dateFormat.format(doc['YOUR_TIME_FIELD'].value);

```

---

<div class="post-metadata">

### Author: ![Hugo\_Pires](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugo_pires/32/68069_2.png) [@Hugo\_Pires](https://discuss.elastic.co/u/Hugo_Pires)
#### Post date: [May 19, 2020, 7:20pm UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/3 "2020-05-19T19:20:50Z")

</div>

Thank you.

Should the scripted field be formatted as date?

---

<div class="post-metadata">

### Author: ![tiagocosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiagocosta/32/40045_2.png) [@tiagocosta](https://discuss.elastic.co/u/tiagocosta)
#### Post date: [May 19, 2020, 8:38pm UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/4 "2020-05-19T20:38:38Z")

</div>

Hey @Hugo_Pires

Yes it should.  
I've built an example with some demo data that you can check in the following image:

 ![Screenshot 2020-05-19 at 21.36.33](https://us1.discourse-cdn.com/elastic/original/3X/6/4/646cac843dc146cee96e3318babde3ba50f0b97d.png)

In my demo data timestamp was of a difference type so I've followed other approach.

---

<div class="post-metadata">

### Author: ![Hugo\_Pires](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugo_pires/32/68069_2.png) [@Hugo\_Pires](https://discuss.elastic.co/u/Hugo_Pires)
#### Post date: [May 20, 2020, 12:13pm UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/5 "2020-05-20T12:13:02Z")

</div>

Hello again and thank you

In both solutions, I got a script error... Could you help?

---

<div class="post-metadata">

### Author: ![tiagocosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiagocosta/32/40045_2.png) [@tiagocosta](https://discuss.elastic.co/u/tiagocosta)
#### Post date: [May 20, 2020, 1:36pm UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/6 "2020-05-20T13:36:33Z")

</div>

Hi,

The final solution will depend on the underlying type of the field for which you're applying the scripted field against.  
Could you share more information about that field and the script error you're facing?

Cheers

---

<div class="post-metadata">

### Author: ![Hugo\_Pires](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugo_pires/32/68069_2.png) [@Hugo\_Pires](https://discuss.elastic.co/u/Hugo_Pires)
#### Post date: [May 20, 2020, 2:48pm UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/7 "2020-05-20T14:48:20Z")

</div>

Sure

The underlying field is a **number** representing the value in seconds. I want to show it in a HH:mm:ss format

The error is:

```auto
"caused_by": {
     "type": "illegal_state_exception",
     "reason": "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
    }

```

---

<div class="post-metadata">

### Author: ![Hugo\_Pires](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugo_pires/32/68069_2.png) [@Hugo\_Pires](https://discuss.elastic.co/u/Hugo_Pires)
#### Post date: [May 21, 2020, 5:28pm UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/8 "2020-05-21T17:28:15Z")

</div>

I 've used the following code:

```auto
def dateFormat = new SimpleDateFormat("HH:mm:ss");
return doc['call_time'].size()==0 ? '' : dateFormat.format(doc['call_time'].value);

```

but it returns always `00:00:00`

Could you help me?

---

<div class="post-metadata">

### Author: ![tiagocosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiagocosta/32/40045_2.png) [@tiagocosta](https://discuss.elastic.co/u/tiagocosta)
#### Post date: [May 21, 2020, 11:54pm UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/9 "2020-05-21T23:54:19Z")

</div>

@Hugo_Pires that is probably happening because you still have invalid values on `call_time` then when formatted as a date time format will just be `0`.

Could you try something like

```auto
def dateFormat = new SimpleDateFormat("HH:mm:ss");
return doc['call_time'].size() !=0 && doc['call_time'].value > 0 ? dateFormat.format(doc['call_time'].value) : '';

```

Cheers

---

<div class="post-metadata">

### Author: ![Hugo\_Pires](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugo_pires/32/68069_2.png) [@Hugo\_Pires](https://discuss.elastic.co/u/Hugo_Pires)
#### Post date: [May 22, 2020, 7:39am UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/10 "2020-05-22T07:39:35Z")

</div>

sorry, the problem is the same

```auto
call_time: 3
call_time_ts: 00:00:00

```

Thank you

---

<div class="post-metadata">

### Author: ![tiagocosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiagocosta/32/40045_2.png) [@tiagocosta](https://discuss.elastic.co/u/tiagocosta)
#### Post date: [May 26, 2020, 8:51pm UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/11 "2020-05-26T20:51:18Z")

</div>

@Hugo_Pires maybe what you're trying to do is not formatting a timestamp but instead formatting a duration. If that is the case maybe you can just simple do:

```auto
def call_time = doc['call_time'].value;
def duration = call_time >= 0 ? call_time : 0;
def hour = duration / 3600;
def minute = (duration % 3600) / 60;
def sec = duration % 60;
return String.format('%02d:%02d:%02ds', new def[] {hour, minute, sec})

```

Cheers

---

<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 23, 2020, 8:51pm UTC](https://discuss.elastic.co/t/how-can-i-format-a-seconds-value-in-a-table/232576/12 "2020-06-23T20:51:24Z")

</div>

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