# How to get current time as unix timestamp for script use

**URL:** <https://discuss.elastic.co/t/how-to-get-current-time-as-unix-timestamp-for-script-use/240723>\
**Category:** Elasticsearch\
**Created:** [July 10, 2020, 3:40pm UTC](https://discuss.elastic.co/t/how-to-get-current-time-as-unix-timestamp-for-script-use/240723 "2020-07-10T15:40:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quentin\_L\_eilde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quentin_l_eilde/32/71956_2.png) [@Quentin\_L\_eilde](https://discuss.elastic.co/u/Quentin_L_eilde)\
**Post date:** [July 10, 2020, 3:40pm UTC](https://discuss.elastic.co/t/how-to-get-current-time-as-unix-timestamp-for-script-use/240723/1 "2020-07-10T15:40:17Z")

</div>

Hello,

I have three fields:

- date\_start (type: date)
- date\_end (type: date)
- permanent (type: bool)

I would like to return all documents with theses conditions:  
date\_start \<= now AND date\_end \>= now  
OR  
date\_start \<= now AND permanent == true

What is the best way to do that ?

I thought it would be to use a script like this :

```json
{
 "query": {
   "bool": {
     "filter": [
       {
         "script": {
           "script": {
             "source": "((doc['date_start'].value <= params.now) && (doc['date_end'].value >= params.now)) || ((doc['date_start'].value <= params.now) && (doc['permanent'].value == params.permanent))"
           },
           "lang": "painless",
           "params": {
             "now": "1594390526",
             "permanent": true
           }
         }
       }
     ]
   }
 }
}

```

But there is an issue with date types comparison and I don't know how to solve this.  
Thank you

---

<div class="post-metadata">

**Author:** ![Vinayak\_Sapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinayak_sapre/32/45939_2.png) [@Vinayak\_Sapre](https://discuss.elastic.co/u/Vinayak_Sapre)\
**Post date:** [July 10, 2020, 10:13pm UTC](https://discuss.elastic.co/t/how-to-get-current-time-as-unix-timestamp-for-script-use/240723/2 "2020-07-10T22:13:32Z")

</div>

@Quentin_L_eilde  
Since you haven't mentioned format for the date\_start and date\_end, I am assuming default format which is "strict\_date\_optional\_time||epoch\_millis"

1. However your "now" is in seconds. It should be in milliseconds.
2. if param "now" is refers to current timestamp then you can pass word now instead of value.
3. You can use simple range queries instead of script.
4. Optionally you can rewrite query as ((date\_start \<= now) AND (date\_end \>= now OR permanent == true))

So you can use (now = current time)

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "date_start": {
              "lte": "now"
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "range": {
                  "date_end": {
                    "gte": "now"
                  }
                }
              },
              {
                "term": {
                  "permanent": true
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

or this if now is a random time

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "date_start": {
              "lte": 1594390526000
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "range": {
                  "date_end": {
                    "gte": 1594390526000
                  }
                }
              },
              {
                "term": {
                  "permanent": true
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Quentin\_L\_eilde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quentin_l_eilde/32/71956_2.png) [@Quentin\_L\_eilde](https://discuss.elastic.co/u/Quentin_L_eilde)\
**Post date:** [July 15, 2020, 2:31pm UTC](https://discuss.elastic.co/t/how-to-get-current-time-as-unix-timestamp-for-script-use/240723/3 "2020-07-15T14:31:52Z")

</div>

hey !

Thanks for the answer it solves my issue 😉

Thanks

---

<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:** [August 12, 2020, 2:46pm UTC](https://discuss.elastic.co/t/how-to-get-current-time-as-unix-timestamp-for-script-use/240723/4 "2020-08-12T14:46:00Z")

</div>

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