title: How to get hours between Min and Max date in Elasticsearch Aggregation?
body: <p>How can I calculate hours between max and min dates (same tree level of max and min) in Elasticsearch?</p>
  
  <p>My Query:-</p>
  
  <pre><code>{
      "size": 0,
      "query": {
          "bool": {
              "must": []
          }
      },
      "aggs": {
          "group_by_areaId": {
              "terms": {
                  "size": 100000,
                  "field": "areaId.keyword"
              },
              "aggs": {
                  "4m": {
                      "date_histogram": {
                          "field": "timestamp",
                          "format": "yyyy-MM-dd'T'HH:mm:ssZZ",
                          "interval": "4m",
                          "order": {
                              "_key": "asc"
                          }
                      },
                      "aggs": {
                          "maxDate": {
                              "max": {
                                  "field": "timestamp"
                              }
                          },
                          "minDate": {
                              "min": {
                                  "field": "timestamp"
                              }
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>And the response (short) as,  </p>
  
  <pre><code>"aggregations": {
          "group_by_areaId": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                  {
                      "key": "key1",
                      "doc_count": 15,
                      "4m": {
                          "buckets": [
                              {
                                  "key_as_string": "2020-02-12T06:08:00+0000",
                                  "key": 1581487680000,
                                  "doc_count": 3,
                                  "minDate": {
                                      "value": 1.581487847E12,
                                      "value_as_string": "2020-02-12T06:10:47Z"
                                  },
                                  "maxDate": {
                                      "value": 1.58148791E12,
                                      "value_as_string": "2020-02-12T06:11:50Z"
                                  },                             
                                   *// Need hours between maxDate and minDate here
                                   //{
                                   //    "hours" : "0.0175" (maxDate-minDate)
                                   //}*
                              }
                          ]
                      }
                  }
              ]
          }
      }
  </code></pre>
  
  <p>Anyone please help me to find out the solution?
  Thanks in Advance. </p>