# How to update a filed with current date and time in ES5.6?

**URL:** https://discuss.elastic.co/t/how-to-update-a-filed-with-current-date-and-time-in-es5-6/102532
**Category:** Elasticsearch
**Created:** [October 3, 2017, 8:56am UTC](https://discuss.elastic.co/t/how-to-update-a-filed-with-current-date-and-time-in-es5-6/102532 "2017-10-03T08:56:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![vikas\_gopal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikas_gopal/32/47661_2.png) [@vikas\_gopal](https://discuss.elastic.co/u/vikas_gopal)
#### Post date: [October 3, 2017, 8:56am UTC](https://discuss.elastic.co/t/how-to-update-a-filed-with-current-date-and-time-in-es5-6/102532/1 "2017-10-03T08:56:11Z")

</div>

Hi Experts,

I want to update my existing field with current date and time so I am using below srcipt and update by query API to achieve the same but I am not getting the desired result .

```
POST abc-2017.09/_update_by_query
{
  "script": {
    "inline": "ctx._source.created_on = 'datetime.now()'; ctx._source.number = 'INC5523123';",
    "lang": "painless"
  },
  
  "query": {
    "match": {
      "eventId": "95392012643"
    }
  }
}

```

When I saw the output I see below

"created\_on": "datetime.now()",  
"number" : "INC5523123"

It is not filling it as a date . Please help , my field is a text as of now , do I need to change it to date ?

Regards  
VG

---

<div class="post-metadata">

### Author: ![Bernt\_Rostad](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Bernt\_Rostad](https://discuss.elastic.co/u/Bernt_Rostad)
#### Post date: [October 3, 2017, 9:32am UTC](https://discuss.elastic.co/t/how-to-update-a-filed-with-current-date-and-time-in-es5-6/102532/2 "2017-10-03T09:32:52Z")

</div>

Rather than using the fairly slow update query just to modify one field why not use a [pipeline](https://www.elastic.co/guide/en/elasticsearch/reference/current/pipeline.html) to set the processing time as the document gets indexed (or updated). Something like

```
{
    "description" : "Pipeline for setting the document processing time",
    "processors" : [
      {
        "script" : {
          "lang" : "painless",
          "inline" : "DateFormat df = new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'\");\ndf.setTimeZone(TimeZone.getTimeZone(\"UTC\"));\nDate date = new Date();\nctx.proctime = df.format(date);"
        }
      }
    ]
}

```

Here the field "proctime" is created and set to the current time for each document indexed through this pipeline processor. All you need is to pass along the pipeline=\<my\_named\_pipe\> parameter when indexing documents.

---

<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: [October 31, 2017, 9:33am UTC](https://discuss.elastic.co/t/how-to-update-a-filed-with-current-date-and-time-in-es5-6/102532/3 "2017-10-31T09:33:20Z")

</div>

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