# Scripted date field returns Invalid date

**URL:** https://discuss.elastic.co/t/scripted-date-field-returns-invalid-date/82980
**Category:** Kibana
**Created:** [April 20, 2017, 5:27am UTC](https://discuss.elastic.co/t/scripted-date-field-returns-invalid-date/82980 "2017-04-20T05:27:07Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![elasticheart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticheart/32/65189_2.png) [@elasticheart](https://discuss.elastic.co/u/elasticheart)
#### Post date: [April 20, 2017, 5:27am UTC](https://discuss.elastic.co/t/scripted-date-field-returns-invalid-date/82980/1 "2017-04-20T05:27:07Z")

</div>

Hi,

I am using ELK GA 5.0.0. I have a field `logtime`, which contains date and time, and I would like to create a scripted field containing only time (or date), for that I made a `Date` scripted field showing the time alone. The settings are like below;

```
Language -> Painless
Type -> Date
Format -> Date
moment.js format pattern -> HH:mm:ss
Popularity -> 0
Script ->
    def dateFormat = new SimpleDateFormat("HH:mm:ss");
    return dateFormat.format(doc['logtime'].value);

```

What I am trying to accomplish is to get a field with value like `23:59:59`, but I am getting an error in Discover, like `Invalid date` for date field. How can I fix this?

Thanks in advance..

---

<div class="post-metadata">

### Author: ![GautamP](https://avatars.discourse-cdn.com/v4/letter/g/ac91a4/32.png) [@GautamP](https://discuss.elastic.co/u/GautamP)
#### Post date: [April 20, 2017, 9:23am UTC](https://discuss.elastic.co/t/scripted-date-field-returns-invalid-date/82980/2 "2017-04-20T09:23:30Z")

</div>

An alternate solution to creating a scripted field would be to go the `Management` tab and change the format of the desired field in the respective index.

Here is a screenshot:

 ![](https://us1.discourse-cdn.com/elastic/original/3X/d/b/dbe0908275c05285b19cd5e3a2a1f6d67c9a1f14.PNG)

Change the `Format` from `-default-` to `Date`.  
Change `moment.js format pattern` to the desired pattern.

---

<div class="post-metadata">

### Author: ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)
#### Post date: [April 20, 2017, 8:25pm UTC](https://discuss.elastic.co/t/scripted-date-field-returns-invalid-date/82980/3 "2017-04-20T20:25:48Z")

</div>

You're getting an error because `23:59:59` is actually a string, not a date. So you'll have to change the type to "string".

Thanks,  
CJ

---

<div class="post-metadata">

### Author: ![elasticheart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticheart/32/65189_2.png) [@elasticheart](https://discuss.elastic.co/u/elasticheart)
#### Post date: [April 21, 2017, 12:11pm UTC](https://discuss.elastic.co/t/scripted-date-field-returns-invalid-date/82980/4 "2017-04-21T12:11:46Z")

</div>

Hi @cjcenizal agrred. But, if I make it a "string", will I be able to pick it as date histogram (first column in my table visualization) and sort my table based on that?

---

<div class="post-metadata">

### Author: ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)
#### Post date: [April 21, 2017, 3:21pm UTC](https://discuss.elastic.co/t/scripted-date-field-returns-invalid-date/82980/5 "2017-04-21T15:21:46Z")

</div>

Oh, good point. I don't think that will work, though it might be worth trying it out and making sure. Have you tried Gautam's solution?

---

<div class="post-metadata">

### Author: ![elasticheart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticheart/32/65189_2.png) [@elasticheart](https://discuss.elastic.co/u/elasticheart)
#### Post date: [April 22, 2017, 6:59pm UTC](https://discuss.elastic.co/t/scripted-date-field-returns-invalid-date/82980/6 "2017-04-22T18:59:50Z")

</div>

Hi @cjcenizal , I don't think that's applicable, because I have some line charts, which needs HH:mm:s. 😞

---

<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, 2017, 6:59pm UTC](https://discuss.elastic.co/t/scripted-date-field-returns-invalid-date/82980/7 "2017-05-20T18:59:55Z")

</div>

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