# Scripted fields to convert seconds to hours

**URL:** https://discuss.elastic.co/t/scripted-fields-to-convert-seconds-to-hours/261790
**Category:** Kibana
**Created:** [January 21, 2021, 1:44pm UTC](https://discuss.elastic.co/t/scripted-fields-to-convert-seconds-to-hours/261790 "2021-01-21T13:44:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ykara84](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ykara84/32/106391_2.png) [@ykara84](https://discuss.elastic.co/u/ykara84)
#### Post date: [January 21, 2021, 1:44pm UTC](https://discuss.elastic.co/t/scripted-fields-to-convert-seconds-to-hours/261790/1 "2021-01-21T13:44:16Z")

</div>

Cant figure it out, need an additional field that will convert seconds into hours

I have a number field called Session-Time which i need converting into hours and displayed in another field. I have tried to create a scripted field with the following painless lines but keeps saying error in script

`doc['Session-Time'].value.hours`

or

`doc['Session-Time'].value / 3600`

the preview just says error in script and then points to the dot in .value

---

<div class="post-metadata">

### Author: ![BenB196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benb196/32/83401_2.png) [@BenB196](https://discuss.elastic.co/u/BenB196)
#### Post date: [January 21, 2021, 1:52pm UTC](https://discuss.elastic.co/t/scripted-fields-to-convert-seconds-to-hours/261790/2 "2021-01-21T13:52:01Z")

</div>

Try adding the word return prior to:

```auto
return doc['Session-Time'].value / 3600

```

Also, another option would be to add a scripted field that is just:

```auto
return doc['Session-Time'].value

```

And then using Kibana's input and output formats to convert the number from hours to seconds.

---

<div class="post-metadata">

### Author: ![ykara84](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ykara84/32/106391_2.png) [@ykara84](https://discuss.elastic.co/u/ykara84)
#### Post date: [January 21, 2021, 1:56pm UTC](https://discuss.elastic.co/t/scripted-fields-to-convert-seconds-to-hours/261790/3 "2021-01-21T13:56:41Z")

</div>

thanks, I did try the return at the beginning but still same error.

forgot to mention the Session-Time is now always populated with a value, so does this mean i have to add an If statement in to check if the fields is populated before performing the math

just seen in the preview more detail re: the error "a document doesn't have a value for a field..."

---

<div class="post-metadata">

### Author: ![BenB196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benb196/32/83401_2.png) [@BenB196](https://discuss.elastic.co/u/BenB196)
#### Post date: [January 21, 2021, 2:01pm UTC](https://discuss.elastic.co/t/scripted-fields-to-convert-seconds-to-hours/261790/4 "2021-01-21T14:01:12Z")

</div>

Ah, yes, I would try adding an if statement if the value at one point was not always populated.

Example:

```auto
if (!doc['Session-Time'].empty) {
    ...
}

```

---

<div class="post-metadata">

### Author: ![ykara84](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ykara84/32/106391_2.png) [@ykara84](https://discuss.elastic.co/u/ykara84)
#### Post date: [January 21, 2021, 2:04pm UTC](https://discuss.elastic.co/t/scripted-fields-to-convert-seconds-to-hours/261790/5 "2021-01-21T14:04:20Z")

</div>

fantastic, that worked

---

<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: [February 18, 2021, 2:04pm UTC](https://discuss.elastic.co/t/scripted-fields-to-convert-seconds-to-hours/261790/6 "2021-02-18T14:04:29Z")

</div>

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