Convert Number into Time (HH:MM:SS)

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?

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;
}"""
    }
  }
}