# Help to add new field from @timestamp

**URL:** <https://discuss.elastic.co/t/help-to-add-new-field-from-timestamp/152202>\
**Category:** Elasticsearch\
**Created:** [October 12, 2018, 9:21am UTC](https://discuss.elastic.co/t/help-to-add-new-field-from-timestamp/152202 "2018-10-12T09:21:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![florentmair](https://avatars.discourse-cdn.com/v4/letter/f/8e7dd6/32.png) [@florentmair](https://discuss.elastic.co/u/florentmair)\
**Post date:** [October 12, 2018, 9:21am UTC](https://discuss.elastic.co/t/help-to-add-new-field-from-timestamp/152202/1 "2018-10-12T09:21:35Z")

</div>

Hello,

i am not able to add new field, i want to date the hourofday value on a new field, but i have this error  
I don't find into the documentation why it doesn't work

```
POST logstash-eno-2018.10_v2/_update_by_query
{      
  "query": {
    "bool": {
      "must_not": [
        {"exists": {
      "field": "eno_heure_utc"
      }}
      ]
    }
  },
  "script": {
    "source" : "ctx._source.eno_heure_utc = ctx._source['@timestamp'].value.hourOfDay",
    "lang": "painless"
    
  }
}

```

i have this error message :

```
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "ctx._source.eno_heure_utc = ctx._source['@timestamp'].value.hourOfDay",
          " ^---- HERE"
        ],
        "script": "ctx._source.eno_heure_utc = ctx._source['@timestamp'].value.hourOfDay",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.eno_heure_utc = ctx._source['@timestamp'].value.hourOfDay",
      " ^---- HERE"
    ],
    "script": "ctx._source.eno_heure_utc = ctx._source['@timestamp'].value.hourOfDay",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Unable to find dynamic field [value] for class [java.lang.String]."
    }
  },
  "status": 500
} 

```

Thanks for help

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 12, 2018, 11:49am UTC](https://discuss.elastic.co/t/help-to-add-new-field-from-timestamp/152202/2 "2018-10-12T11:49:44Z")

</div>

If you get a date field from the `_source` it will be returned as a string. You need to parse that string into a date first, before you can get the hour of day. The following script should work:

```auto
ctx._source.eno_heure_utc = OffsetDateTime.parse(ctx._source['@timestamp']).getHour()

```

---

<div class="post-metadata">

**Author:** ![florentmair](https://avatars.discourse-cdn.com/v4/letter/f/8e7dd6/32.png) [@florentmair](https://discuss.elastic.co/u/florentmair)\
**Post date:** [October 12, 2018, 1:19pm UTC](https://discuss.elastic.co/t/help-to-add-new-field-from-timestamp/152202/3 "2018-10-12T13:19:24Z")

</div>

thanks it works,

if someone needs for the number of Day of week

```
"source" : "ctx._source.eno_weekday = OffsetDateTime.parse(ctx._source['@timestamp']).DayOfWeek.getValue()"

```

it's not easy to find this information into the official documentation

---

<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:** [November 9, 2018, 1:19pm UTC](https://discuss.elastic.co/t/help-to-add-new-field-from-timestamp/152202/4 "2018-11-09T13:19:25Z")

</div>

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