# Get Current Time and date

**URL:** <https://discuss.elastic.co/t/get-current-time-and-date/117459>\
**Category:** Elasticsearch\
**Created:** [January 29, 2018, 1:05pm UTC](https://discuss.elastic.co/t/get-current-time-and-date/117459 "2018-01-29T13:05:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![devil\_srj7](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@devil\_srj7](https://discuss.elastic.co/u/devil_srj7)\
**Post date:** [January 29, 2018, 1:05pm UTC](https://discuss.elastic.co/t/get-current-time-and-date/117459/1 "2018-01-29T13:05:17Z")

</div>

I need to retrieve all the records between **Start of the day to End of the day**

For Eg: `2018-01-29 00:00:00 - 2018-01-29 23:59:59`

Similarly, I need to get current Time and Date.

```
GET _search
{
  "size": 1, 
  "script_fields": {
    "now": {
      "script": "new Date().getTime()"
    }
  }
}

```

But this gives me the result as

```
{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 76,
    "successful": 76,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 17257117,
    "max_score": 1,
    "hits": [
      {
        "_index": ".kibana",
        "_type": "doc",
        "_id": "index-pattern:c23523c0-f6b7-11e7-864f-3d5a306a7451",
        "_score": 1,
        "fields": {
          "now": [
            1517230755079
          ]
        }
      }
    ]
  }
}

```

But the required Format is

```
{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 76,
    "successful": 76,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 17257117,
    "max_score": 1,
    "hits": [
      {
        "_index": ".kibana",
        "_type": "doc",
        "_id": "index-pattern:c23523c0-f6b7-11e7-864f-3d5a306a7451",
        "_score": 1,
        "fields": {
          "now": [
            2018-01-29 18:33:42
          ]
        }
      }
    ]
  }
}
```

---

<div class="post-metadata">

**Author:** ![Mojster](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mojster/32/21209_2.png) [@Mojster](https://discuss.elastic.co/u/Mojster)\
**Post date:** [January 29, 2018, 1:31pm UTC](https://discuss.elastic.co/t/get-current-time-and-date/117459/2 "2018-01-29T13:31:35Z")

</div>

If you need to get all records, you should use `constant_query` it's allot faster than normal query. Because it doesn't add weight so search.

My Example:

```auto
GET index/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "date": {
                  "gte": "2018-01-29",
                  "lte": "2018-01-29",
                  "format": "YYYY-dd-MM",
                  "time_zone": "Europe/..."
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

`Script_fields` are not for searching, they are for postprocessing search results:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html)

---

<div class="post-metadata">

**Author:** ![Magnus\_Kessler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnus_kessler/32/42001_2.png) [@Magnus\_Kessler](https://discuss.elastic.co/u/Magnus_Kessler)\
**Post date:** [January 29, 2018, 3:46pm UTC](https://discuss.elastic.co/t/get-current-time-and-date/117459/3 "2018-01-29T15:46:12Z")

</div>

Have a look at [Date Math](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math). If you wanted to hit records for the current day, you could use

```auto
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "@timestamp": {
            "gte": "now/d",
            "lt": "now+1d/d"
          }
        }
      }
    }
  }
}

```

or for the previous day

```auto
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "@timestamp": {
            "gte": "now-1d/d",
            "lt": "now/d"
          }
        }
      }
    }
  }
}

```

To iterate over all records, I'd recommend using the [Scroll API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html).

---

<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 26, 2018, 3:46pm UTC](https://discuss.elastic.co/t/get-current-time-and-date/117459/4 "2018-02-26T15:46:28Z")

</div>

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