# How do I use dayOfWeek in a Watcher condition script?

**URL:** <https://discuss.elastic.co/t/how-do-i-use-dayofweek-in-a-watcher-condition-script/38341>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [January 4, 2016, 6:59pm UTC](https://discuss.elastic.co/t/how-do-i-use-dayofweek-in-a-watcher-condition-script/38341 "2016-01-04T18:59:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnMunson](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@JohnMunson](https://discuss.elastic.co/u/JohnMunson)\
**Post date:** [January 4, 2016, 6:59pm UTC](https://discuss.elastic.co/t/how-do-i-use-dayofweek-in-a-watcher-condition-script/38341/1 "2016-01-04T18:59:10Z")

</div>

Hi. I've been using Watcher for a little while, but I don't know much about Elasticsearch itself -- enough to construct the queries I've needed for my watches (many thanks to Steve Kearns for his help when I was getting started). I've done a lot of looking around and haven't been able to figure this out. I want my condition script to check the day of the week for a particular hit. The date is stored in a field whose definition is:

"dateTransaction" : {  
"type" : "date",  
"format" : "dateOptionalTime"  
}

I found references online to doing things like this to include the day of the week in a query...

"script": "doc['@timestamp'].date.dayOfWeek == 7"

... and I tried to adapt that to get the day of the week from our dateTransaction field in the watch's condition script, but without success. The underlying problem seems to be that the condition script sees dateTransaction as a string rather than as a date. Is there a way to get the condition script to see it as a date, and if so, what's the syntax to get the day of the week from it?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 5, 2016, 8:29am UTC](https://discuss.elastic.co/t/how-do-i-use-dayofweek-in-a-watcher-condition-script/38341/2 "2016-01-05T08:29:25Z")

</div>

Heya,

I think what you found might refer to older versions. However, if you use groovy, you have full access to the joda time library and this makes it a breeze to write your own logic for this. Take this example, but make sure you check out the joda docs!

```auto
PUT /test/test/1
{
  "day": "Tuesday"
}

PUT /test/test/2
{
  "day": "Wednesday"
}

GET /test/test/_mapping

GET test/_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": "doc['day'].value == DateTime.now().dayOfWeek().getAsText(Locale.ROOT).toLowerCase(Locale.ROOT)"
        }
      }
    }
  }
}

```

Instead of `DateTime.now()`, you can also just pass the date of a field like `new DateTime(doc['@timestamp'])`

Hope this helps! On further questions, please provide a full example of what you did with watcher and conditions, so one can reproduce. Thx!

--Alex

---

<div class="post-metadata">

**Author:** ![JohnMunson](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@JohnMunson](https://discuss.elastic.co/u/JohnMunson)\
**Post date:** [January 5, 2016, 8:46pm UTC](https://discuss.elastic.co/t/how-do-i-use-dayofweek-in-a-watcher-condition-script/38341/3 "2016-01-05T20:46:37Z")

</div>

Alex, thanks so much. The DateTime class was the missing piece I needed. Much appreciated!

---

<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:** [July 6, 2017, 1:47pm UTC](https://discuss.elastic.co/t/how-do-i-use-dayofweek-in-a-watcher-condition-script/38341/4 "2017-07-06T13:47:26Z")

</div>


