# Get the difference between different format of datetime

**URL:** <https://discuss.elastic.co/t/get-the-difference-between-different-format-of-datetime/257325>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [December 2, 2020, 9:06am UTC](https://discuss.elastic.co/t/get-the-difference-between-different-format-of-datetime/257325 "2020-12-02T09:06:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Izek\_Chen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/izek_chen/32/78257_2.png) [@Izek\_Chen](https://discuss.elastic.co/u/Izek_Chen)\
**Post date:** [December 2, 2020, 9:06am UTC](https://discuss.elastic.co/t/get-the-difference-between-different-format-of-datetime/257325/1 "2020-12-02T09:06:06Z")

</div>

I have data with two date field like this

```auto
 "@buildTimestamp": "2020-12-01T18:55:34.895-0800",
 "@timestamp": "2020-12-02T03:02:16.723Z"

```

I'd like to use ingest pipeline script processor to get the difference.  
How can I converse this two to the same format, then do the comparison?  
I cannot get it right

I tried something like this

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [

      {
        "script": {
          "lang": "painless",
          "source": """
            def test;
            test = ChronoUnit.MILLIS.between(ZonedDateTime.parse(ctx['@buildTimestamp']), ZonedDateTime.parse(ctx['@timestamp']))/1000;
          """
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "test-2020.12",
      "_type": "_doc",
      "_id": "ojlmIXYBFr9SJ001ii0C",
      "_source": {
        "@buildTimestamp": "2020-12-01T18:55:34.895-0800",
        "@timestamp": "2020-12-02T03:02:16.723Z"
      }
    }
  ]
}

```

but the error shows

```auto
{
  "docs" : [
    {
      "error" : {
        "root_cause" : [
          {
            "type" : "exception",
            "reason" : "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: DateTimeParseException[Text '2020-12-01T18:55:34.895-0800' could not be parsed, unparsed text found at index 26];",
            "header" : {
              "processor_type" : "script"
            }
          }
        ],
        "type" : "exception",
        "reason" : "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: DateTimeParseException[Text '2020-12-01T18:55:34.895-0800' could not be parsed, unparsed text found at index 26];",
        "caused_by" : {
          "type" : "illegal_argument_exception",
          "reason" : "ScriptException[runtime error]; nested: DateTimeParseException[Text '2020-12-01T18:55:34.895-0800' could not be parsed, unparsed text found at index 26];",
          "caused_by" : {
            "type" : "script_exception",
            "reason" : "runtime error",
            "script_stack" : [
              "java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)",
              "java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1951)",
              "java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)",
              "java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:583)",
              "test = ChronoUnit.MILLIS.between(ZonedDateTime.parse(ctx['@buildTimestamp']), ZonedDateTime.parse(ctx['@timestamp']))/1000;",
              " ^---- HERE"
            ],
            "script" : " def test;\n test = ChronoUnit.MILLIS.between(ZonedDateTime.parse(ctx['@buildTimestamp']), ZonedDateTime.parse(ctx['@timestamp']))/1000;",
            "lang" : "painless",
            "caused_by" : {
              "type" : "date_time_parse_exception",
              "reason" : "Text '2020-12-01T18:55:34.895-0800' could not be parsed, unparsed text found at index 26"
            }
          }
        },
        "header" : {
          "processor_type" : "script"
        }
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![Izek\_Chen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/izek_chen/32/78257_2.png) [@Izek\_Chen](https://discuss.elastic.co/u/Izek_Chen)\
**Post date:** [December 3, 2020, 2:10pm UTC](https://discuss.elastic.co/t/get-the-difference-between-different-format-of-datetime/257325/2 "2020-12-03T14:10:41Z")

</div>

Nevermind, I get it working  
something like this

```auto
def buildts = ctx['@buildTimestamp'];
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("[yyyyMMdd][yyyy-MM-dd][yyyy-DDD]['T'[HHmmss][HHmm][HH:mm:ss][HH:mm][.SSS][XXXX]");
ZonedDateTime zdt1 = ZonedDateTime.parse(buildts, dtf);
ZonedDateTime zdt2 = ZonedDateTime.parse(ctx['@timestamp']);
long differencInMillis = ChronoUnit.MILLIS.between(zdt1, zdt2);
ctx['build_duration'] = differencInMillis

```

---

<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:** [December 31, 2020, 2:10pm UTC](https://discuss.elastic.co/t/get-the-difference-between-different-format-of-datetime/257325/3 "2020-12-31T14:10:48Z")

</div>

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