# How to convert a date to string using local time

**URL:** <https://discuss.elastic.co/t/how-to-convert-a-date-to-string-using-local-time/229038>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [April 21, 2020, 12:49pm UTC](https://discuss.elastic.co/t/how-to-convert-a-date-to-string-using-local-time/229038 "2020-04-21T12:49:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel\_Cabero](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel_cabero/32/66766_2.png) [@Miguel\_Cabero](https://discuss.elastic.co/u/Miguel_Cabero)\
**Post date:** [April 21, 2020, 12:49pm UTC](https://discuss.elastic.co/t/how-to-convert-a-date-to-string-using-local-time/229038/1 "2020-04-21T12:49:07Z")

</div>

Hello,  
I need to compute a field that concats a string and a date.  
I don't know how to convert the date using the local time.  
Can you help getting desired results below?

```auto
PUT my_index/x
{
  "ref": "x",
  "date" : "2019-11-15T15:16:16+01:00"
}

PUT my_index/y
{
  "ref": "y",
  "date" : "2019-09-10T00:00:00+02:00"
}

GET my_index/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "ref_date": {
      "script": "doc['ref.keyword'].value + ':' + doc['date'].value.toString('yyyy-MM-dd')"
    }
  }
}

```

ACTUAL RESULTS:

```auto
{
        "_id" : "x",
        "fields" : {
          "ref_date" : [
            "x:2019-11-15"
          ]
        }
      },
      {
        "_id" : "y",
        "fields" : {
          "ref_date" : [
            "y:2019-09-09"
          ]
        }
      }

```

DESIRED RESULTS:

```auto
{
        "_id" : "x",
        "fields" : {
          "ref_date" : [
            "x:2019-11-15"
          ]
        }
      },
      {
        "_id" : "y",
        "fields" : {
          "ref_date" : [
            "y:2019-09-10"
          ]
        }
      }

```

---

<div class="post-metadata">

**Author:** ![Miguel\_Cabero](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel_cabero/32/66766_2.png) [@Miguel\_Cabero](https://discuss.elastic.co/u/Miguel_Cabero)\
**Post date:** [April 22, 2020, 4:20pm UTC](https://discuss.elastic.co/t/how-to-convert-a-date-to-string-using-local-time/229038/2 "2020-04-22T16:20:00Z")

</div>

So, this is how I solved it:

```auto
GET my_index/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "ref_date": {
      "script": "doc['ref.keyword'].value + ':' + params['_source']['date'].substring(0,10)"
    }
  }
}

```

---

<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:** [May 20, 2020, 4:20pm UTC](https://discuss.elastic.co/t/how-to-convert-a-date-to-string-using-local-time/229038/3 "2020-05-20T16:20:09Z")

</div>

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