# Week of the year / sunday of the week

**URL:** <https://discuss.elastic.co/t/week-of-the-year-sunday-of-the-week/267529>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [March 17, 2021, 2:21pm UTC](https://discuss.elastic.co/t/week-of-the-year-sunday-of-the-week/267529 "2021-03-17T14:21:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lachlann562](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lachlann562/32/85698_2.png) [@lachlann562](https://discuss.elastic.co/u/lachlann562)\
**Post date:** [March 17, 2021, 2:21pm UTC](https://discuss.elastic.co/t/week-of-the-year-sunday-of-the-week/267529/1 "2021-03-17T14:21:39Z")

</div>

Week of the year is challenging when handling year rolling, etc. a previous article was:  
[Week of the year aggregations using Painless - Elastic Stack / Elasticsearch - Discuss the Elastic Stack](https://discuss.elastic.co/t/week-of-the-year-aggregations-using-painless/140071/5)

I found a simple solution to this problem, rather trying tog et week of the year, i decided to standardize on the "sunday of the week" this avoids all sorts of issue with leap years, rolling years, etc. its also a very simple script.

`doc['@timestamp'].value.plusDays(-1*(doc['@timestamp'].value.dayOfWeek)).truncatedTo(ChronoUnit.DAYS)`

---

<div class="post-metadata">

**Author:** ![Marta\_Bondyra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marta_bondyra/32/102122_2.png) [@Marta\_Bondyra](https://discuss.elastic.co/u/Marta_Bondyra)\
**Post date:** [March 30, 2021, 9:58am UTC](https://discuss.elastic.co/t/week-of-the-year-sunday-of-the-week/267529/2 "2021-03-30T09:58:53Z")

</div>

That's a great solution, thank you!

---

<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:** [April 27, 2021, 9:59am UTC](https://discuss.elastic.co/t/week-of-the-year-sunday-of-the-week/267529/3 "2021-04-27T09:59:25Z")

</div>

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