# Convert Number into Time (HH:MM:SS)

**URL:** https://discuss.elastic.co/t/convert-number-into-time-hhss/235494
**Category:** Kibana
**Created:** [June 3, 2020, 8:17am UTC](https://discuss.elastic.co/t/convert-number-into-time-hhss/235494 "2020-06-03T08:17:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Thibaut\_M](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@Thibaut\_M](https://discuss.elastic.co/u/Thibaut_M)
#### Post date: [June 3, 2020, 8:17am UTC](https://discuss.elastic.co/t/convert-number-into-time-hhss/235494/1 "2020-06-03T08:17:17Z")

</div>

Hello,  
I'm refering to my previous post : [Convert Number (elapsed\_time) into Time (HH:MM:SS)](https://discuss.elastic.co/t/convert-number-elapsed-time-into-time-hhss/218551/3)

I have some issue that i don't understand with the scripted field from angelo response. This time I have a number in Seconds and I want it to be a duration like that hh:mm:ss (in my previous topic i had number in milliseconds)

I have come with this

```auto
if (doc['duration'].size()==0) {
    return ""
} else {
    long elapsed_time=(long)doc["duration"].value;
    
    
    long minutesInseconds = 60;
    long hoursInseconds = minutesInseconds * 60;
    // long daysInMilli = hoursInMilli * 24;

    // long elapsedDays = elapsed_time / daysInMilli;
    // elapsed_time = elapsed_time % daysInMilli;
	
    long elapsedHours = elapsed_time / hoursInseconds;
    elapsed_time = elapsed_time % hoursInseconds;

    long elapsedMinutes = elapsed_time / minutesInseconds;
    elapsed_time = elapsed_time % minutesInseconds;
    
    long elapsedSeconds = elapsed_time;

    String my_answer = String.format("%02d:%02d:%02d", new def[] {(int)elapsedHours, (int)elapsedMinutes, (int)elapsedSeconds});
    return my_answer;
}

```

But for example if i set 7200 second (2 hours) it's displayed like that 00:00:02 instead of 02:00:00

Can tell me where is my mistake here because I don't understand ?

In my scripted field i have correctly set up my number like this :

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/b/7bcc5b761f73312946d6c6ea297e63e77c6c1b26.png)

Thank you for help  
Thibaut

---

<div class="post-metadata">

### Author: ![Nathan\_Reese](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_reese/32/84829_2.png) [@Nathan\_Reese](https://discuss.elastic.co/u/Nathan_Reese)
#### Post date: [June 3, 2020, 1:49pm UTC](https://discuss.elastic.co/t/convert-number-into-time-hhss/235494/2 "2020-06-03T13:49:18Z")

</div>

You are really close. Just set format as `string` instead of `number` since your script is returning a string and not a number.

 ![Screen Shot 2020-06-03 at 7.47.23 AM](https://us1.discourse-cdn.com/elastic/original/3X/a/5/a54ceb0bc61236c1b175d1fe92f350c9a280729d.png)

Here is the sample data I was using

```auto
PUT test
{}

PUT test/_doc/1
{
    "duration": 7200
}

PUT test/_doc/2
{
    "duration": 3900
}

```

---

<div class="post-metadata">

### Author: ![Thibaut\_M](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@Thibaut\_M](https://discuss.elastic.co/u/Thibaut_M)
#### Post date: [June 5, 2020, 7:24am UTC](https://discuss.elastic.co/t/convert-number-into-time-hhss/235494/3 "2020-06-05T07:24:02Z")

</div>

Hi Nathan,

I tried what you suggest but as you can see in my screenshot

![image](https://us1.discourse-cdn.com/elastic/original/3X/3/e/3ecdb17c69c696a68f830dcc1492924997c6a2de.png)

It convert in Minute:seconds:milliseconds I don' have the hours... Do you know if there is a mistake in my scripted field ?

Thibaut

---

<div class="post-metadata">

### Author: ![angelo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/angelo/32/61325_2.png) [@angelo](https://discuss.elastic.co/u/angelo)
#### Post date: [June 13, 2020, 2:46am UTC](https://discuss.elastic.co/t/convert-number-into-time-hhss/235494/4 "2020-06-13T02:46:35Z")

</div>

I'm not sure where this is going wrong. In my case, a 'duration' of 7200 always returns "02:00:00" given the format specified at the end for `my_answer`. This is regardless of asking Kibana to format it as a Number or String (with Number.js formatting of 00:00:00).

However, your last screenshot is a bit curious - if the `duration` value is 7200, you are showing a field of `duration_min`, which on its own would be equal to 120 minutes if you wanted to report it that way. If you do want it in HH:mm:ss format, then that should come out to 02:00:00 which is done via the string formatting of `my_answer`.

As a test, if you run the following in Kibana Dev Tools, do you get values of "02:00:00" and "01:05:00" using Nathan's example?

```auto
PUT test
{}
PUT test/_doc/1
{"elapsed_time": 7200}
PUT test/_doc/2
{"elapsed_time": 3900}
GET test/_search
{
  "script_fields": {
    "diff": {
      "script": """if (doc['elapsed_time'].size()==0) {
    return ""
} else {
    long elapsed_time=(long)doc["elapsed_time"].value;
    
    long minutesInseconds = 60;
    long hoursInseconds = minutesInseconds * 60;
    // long daysInMilli = hoursInMilli * 24;

    // long elapsedDays = elapsed_time / daysInMilli;
    // elapsed_time = elapsed_time % daysInMilli;
	
    long elapsedHours = elapsed_time / hoursInseconds;
    elapsed_time = elapsed_time % hoursInseconds;

    long elapsedMinutes = elapsed_time / minutesInseconds;
    elapsed_time = elapsed_time % minutesInseconds;
    
    long elapsedSeconds = elapsed_time;

    String my_answer = String.format("%02d:%02d:%02d", new def[] {(int)elapsedHours, (int)elapsedMinutes, (int)elapsedSeconds});
    return my_answer;
}"""
    }
  }
}

```

---

<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: [July 11, 2020, 2:46am UTC](https://discuss.elastic.co/t/convert-number-into-time-hhss/235494/5 "2020-07-11T02:46:36Z")

</div>

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