9

What is the timezone of elasticsearch when I use now ?

{
   "range": {
      "myDateInSomeTimezone": {
         "gt": "now"
      }
   }
},

I am wondering what is the timezone of now:

  • is with my server local timezone?
  • is with same timezone of the field?
  • is with UTC timezone?
Alberto
  • 2,881
  • 7
  • 35
  • 66

2 Answers2

7

now is always resolved to unix timestamp in millisecond. So one would need to either store the dates in UTC or speicify the timezone parameter in the range query.

From the documentation :

Dates can be converted from another timezone to UTC either by specifying the time zone in the date value itself (if the format accepts it), or it can be specified as the time_zone parameter:

now is not affected by the time_zone parameter (dates must be stored as UTC).

Alberto
  • 2,881
  • 7
  • 35
  • 66
keety
  • 17,231
  • 4
  • 51
  • 56
  • 1
    I have a similar problem, and looking for a way to tell ES that consider now in my current timezone not UTC, do you know how can I tell ES to consider now in my timezone in all queries that ES handle? – Yuseferi Dec 20 '17 at 09:52
  • This is my problem too, for tests the Timezone are using user machine TZ :( – Fernando Almeida Jun 27 '19 at 13:16
  • The dates are already stored in UTC, in ElasticSearch – hyankov May 11 '21 at 17:07
4

now by itself will query based on UTC. So querying documents from midnight UTC to now would look like:

         "range" =>  {
            "myDateInSomeTimezone" =>  {
              "gte" =>  "now/d",
              "lte" =>  "now"
            }
          }

If you want to query documents in timezone other than UTC, you can use the time_zone parameter to the range queries like this:

         "range" =>  {
            "myDateInSomeTimezone" =>  {
              "gte" =>  "now/d",
              "lte" =>  "now",
              "time_zone" =>  "America/Los_Angeles"
            }
          }
James Boutcher
  • 2,593
  • 1
  • 25
  • 37