# Create script - convert date to weeknumber

**URL:** <https://discuss.elastic.co/t/create-script-convert-date-to-weeknumber/359047>\
**Category:** Kibana\
**Created:** [May 8, 2024, 10:20am UTC](https://discuss.elastic.co/t/create-script-convert-date-to-weeknumber/359047 "2024-05-08T10:20:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [May 8, 2024, 10:20am UTC](https://discuss.elastic.co/t/create-script-convert-date-to-weeknumber/359047/1 "2024-05-08T10:20:49Z")

</div>

Hello,

I have been trying to create scripted field tha twoul convert date into week number but w/o success. I tried with this. Does not work.

doc['planDate'].value.getWeekNum()

I would appreciate some help.

Thanks!

---

<div class="post-metadata">

**Author:** ![jessgarson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jessgarson/32/129841_2.png) [@jessgarson](https://discuss.elastic.co/u/jessgarson)\
**Post date:** [May 10, 2024, 2:43pm UTC](https://discuss.elastic.co/t/create-script-convert-date-to-weeknumber/359047/2 "2024-05-10T14:43:18Z")

</div>

Hi @Valerija,

Thanks for reaching out. What format are you expecting to see, and how is your date formatted currently? You may also want to check out our [documentation on formatting](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html).

Best,  
Jessica

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [May 14, 2024, 8:22am UTC](https://discuss.elastic.co/t/create-script-convert-date-to-weeknumber/359047/3 "2024-05-14T08:22:40Z")

</div>

Hi,

Date is formatted as a date. What I would like to see is week number - for eg. CW 25 but I am not sure how to write a script to get the data is this format.

Thanks.  
Violeta

---

<div class="post-metadata">

**Author:** ![jessgarson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jessgarson/32/129841_2.png) [@jessgarson](https://discuss.elastic.co/u/jessgarson)\
**Post date:** [May 14, 2024, 11:07pm UTC](https://discuss.elastic.co/t/create-script-convert-date-to-weeknumber/359047/4 "2024-05-14T23:07:33Z")

</div>

Thanks for following up, @Valerija. Would [ZonedDateTime](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-api-reference-shared-java-time.html#painless-api-reference-shared-ZonedDateTime) work here? My first thought would be trying something similar to this:

```auto
POST /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": {
              "source": """
                ZonedDateTime date = doc['planDate'].value.toInstant().atZone(ZoneId.of('UTC'));
                int week = date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
                int year = date.getYear();
                return week == 25 && year == 2024;
              """,
              "lang": "painless"
            }
          }
        }
      ]
    }
  }
}

```
