# Having hard time to extract day of week in scripted field

**URL:** https://discuss.elastic.co/t/having-hard-time-to-extract-day-of-week-in-scripted-field/281690
**Category:** Kibana
**Created:** [August 17, 2021, 1:45pm UTC](https://discuss.elastic.co/t/having-hard-time-to-extract-day-of-week-in-scripted-field/281690 "2021-08-17T13:45:09Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Mojahid\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mojahid_ahmad/32/78061_2.png) [@Mojahid\_Ahmad](https://discuss.elastic.co/u/Mojahid_Ahmad)
#### Post date: [August 17, 2021, 1:45pm UTC](https://discuss.elastic.co/t/having-hard-time-to-extract-day-of-week-in-scripted-field/281690/1 "2021-08-17T13:45:09Z")

</div>

I am able to extract hour of day using the below script format. But facing the issue while extracting the day of week because of timezone.

I used this script.

`["","Mon","Tue","Wed","Thu","Fri","Sat","Sun"]doc['event_time'].value.dayOfWeek`

You can see the July 23 extract Fri and Thu as a day of week.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/f/3f7914ad82f9e96f3e616bf52883e0b45b0aba5f.png)

So please guide how to resolve this issue? Thank you

Continuing the discussion from [Hour Scripted Field](https://discuss.elastic.co/t/hour-scripted-field/263416/5):

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [August 17, 2021, 2:18pm UTC](https://discuss.elastic.co/t/having-hard-time-to-extract-day-of-week-in-scripted-field/281690/2 "2021-08-17T14:18:54Z")

</div>

This is done in Painless Lab so will need to change for a scripted field.

```auto
# Get the date and store in ZonedDateTime variable.
ZonedDateTime zdt = ZonedDateTime.parse(params.date_field);

# Set the pattern which is day of week. See documentation below.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E");

# Extract the day of the week and save as a string.
String day_of_week = zdt.format(formatter);

# Output the day of week.
return day_of_week;

```

**Input**  
`"date_field": "2021-06-12T09:28:48+00:00"`

**Output**  
`Sat`

[Documentation](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html)

---

<div class="post-metadata">

### Author: ![Mojahid\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mojahid_ahmad/32/78061_2.png) [@Mojahid\_Ahmad](https://discuss.elastic.co/u/Mojahid_Ahmad)
#### Post date: [August 17, 2021, 2:35pm UTC](https://discuss.elastic.co/t/having-hard-time-to-extract-day-of-week-in-scripted-field/281690/3 "2021-08-17T14:35:35Z")

</div>

how to do in scripted field ?

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [August 17, 2021, 3:00pm UTC](https://discuss.elastic.co/t/having-hard-time-to-extract-day-of-week-in-scripted-field/281690/4 "2021-08-17T15:00:08Z")

</div>

The below should work. Just replace with your field name. The only thing that changes is where you are getting the data from.

```auto
# Get the date and store in ZonedDateTime variable.
ZonedDateTime zdt = ZonedDateTime.parse(doc['YOUR-FIELD-NAME'].value);

# Set the pattern which is day of week. See documentation below.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E");

# Extract the day of the week and save as a string.
String day_of_week = zdt.format(formatter);

# Output the day of week.
return day_of_week;

```

---

<div class="post-metadata">

### Author: ![Mojahid\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mojahid_ahmad/32/78061_2.png) [@Mojahid\_Ahmad](https://discuss.elastic.co/u/Mojahid_Ahmad)
#### Post date: [August 23, 2021, 6:09am UTC](https://discuss.elastic.co/t/having-hard-time-to-extract-day-of-week-in-scripted-field/281690/5 "2021-08-23T06:09:53Z")

</div>

I am getting this error.

```auto
{
 "root_cause": [
  {
   "type": "script_exception",
   "reason": "runtime error",
   "script_stack": [
    "java.base/java.lang.Class.cast(Class.java:3818)",
    "zdt = ZonedDateTime.parse(doc['fm_data.event_time'].value);\nDateTimeFormatter ",
    " ^---- HERE"
   ],
   "script": "ZonedDateTime zdt = ZonedDateTime.parse(doc['fm_data.event_time'].value); ...",
   "lang": "painless",
   "position": {
    "offset": 65,
    "start": 14,
    "end": 92
   }
  }
 ],
 "type": "search_phase_execution_exception",
 "reason": "all shards failed",
 "phase": "query",
 "grouped": true,
 "failed_shards": [
  {
   "shard": 0,
   "index": "4g-radio-fm",
   "node": "n-2CcZomQIWiklu2KWP19g",
   "reason": {
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
     "java.base/java.lang.Class.cast(Class.java:3818)",
     "zdt = ZonedDateTime.parse(doc['fm_data.event_time'].value);\nDateTimeFormatter ",
     " ^---- HERE"
    ],
    "script": "ZonedDateTime zdt = ZonedDateTime.parse(doc['fm_data.event_time'].value); ...",
    "lang": "painless",
    "position": {
     "offset": 65,
     "start": 14,
     "end": 92
    },
    "caused_by": {
     "type": "class_cast_exception",
     "reason": "Cannot cast org.elasticsearch.script.JodaCompatibleZonedDateTime to java.lang.CharSequence"
    }
   }
  }
 ]
}

```

---

<div class="post-metadata">

### Author: ![Mojahid\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mojahid_ahmad/32/78061_2.png) [@Mojahid\_Ahmad](https://discuss.elastic.co/u/Mojahid_Ahmad)
#### Post date: [August 23, 2021, 7:03am UTC](https://discuss.elastic.co/t/having-hard-time-to-extract-day-of-week-in-scripted-field/281690/6 "2021-08-23T07:03:01Z")

</div>

I tried to use this script. This script is given in the documentation.

> **[Using Painless in Kibana scripted fields](https://www.elastic.co/blog/using-painless-kibana-scripted-fields)**

`LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['fm_data.event_time'].value), ZoneId.of('Asia/Kolkata')).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault())`

Here also I am getting the error. Please see the error below.

```auto
{
 "root_cause": [
  {
   "type": "script_exception",
   "reason": "runtime error",
   "script_stack": [
    "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['fm_data.event_time'].value), ZoneId.of('Asia/Kolkata')).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault())\n\n",
    " ^---- HERE"
   ],
   "script": " ...",
   "lang": "painless",
   "position": {
    "offset": 71,
    "start": 1,
    "end": 175
   }
  }
 ],
 "type": "search_phase_execution_exception",
 "reason": "all shards failed",
 "phase": "query",
 "grouped": true,
 "failed_shards": [
  {
   "shard": 0,
   "index": "4g-radio-fm",
   "node": "n-2CcZomQIWiklu2KWP19g",
   "reason": {
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
     "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['fm_data.event_time'].value), ZoneId.of('Asia/Kolkata')).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault())\n\n",
     " ^---- HERE"
    ],
    "script": " ...",
    "lang": "painless",
    "position": {
     "offset": 71,
     "start": 1,
     "end": 175
    },
    "caused_by": {
     "type": "wrong_method_type_exception",
     "reason": "cannot convert MethodHandle(Dates)JodaCompatibleZonedDateTime to (Object)long"
    }
   }
  }
 ]
}

```

---

<div class="post-metadata">

### Author: ![Mojahid\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mojahid_ahmad/32/78061_2.png) [@Mojahid\_Ahmad](https://discuss.elastic.co/u/Mojahid_Ahmad)
#### Post date: [August 23, 2021, 10:41am UTC](https://discuss.elastic.co/t/having-hard-time-to-extract-day-of-week-in-scripted-field/281690/7 "2021-08-23T10:41:00Z")

</div>

I am able to figure out the solution. This script is perfectly working for me

`LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['event_time'].value.millis), ZoneId.of('Asia/Kolkata')).getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.getDefault())`

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: [September 20, 2021, 10:41am UTC](https://discuss.elastic.co/t/having-hard-time-to-extract-day-of-week-in-scripted-field/281690/8 "2021-09-20T10:41:36Z")

</div>

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