# Scripted field getting 12 hours date time difference

**URL:** <https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999>\
**Category:** Kibana\
**Created:** [September 17, 2020, 3:07pm UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999 "2020-09-17T15:07:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmadsayeed3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadsayeed3/32/72888_2.png) [@ahmadsayeed3](https://discuss.elastic.co/u/ahmadsayeed3)\
**Post date:** [September 17, 2020, 3:07pm UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/1 "2020-09-17T15:07:52Z")

</div>

Hi,

I am trying to get difference between current date time and a field(date time).

Timezone for date formatting: **UTC**  
Date format: **MM/D/YYYY HH:mm:ss.SSS**  
Date with nanoseconds format: **MMM D, YYYY @ HH:mm:ss.SSSSSSSSS**

**Scripted Field:**  
`if(!doc['job_start_time'].empty) return new Date().getTime() / 1000 - doc['job_start_time'].value.millis / 1000`

job\_start\_time: **09/17/2020 02:07:35.379**

I have set Output format: Human Readable

Whenever I am trying to get the difference It's showing +12 hours.

What I am doing wrong here? Please guide.

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [September 17, 2020, 3:36pm UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/2 "2020-09-17T15:36:21Z")

</div>

Hi and welcome to our community

Might be a timezone issue, what's the offset of your timezone to UTC ?

Best,  
Matthias

---

<div class="post-metadata">

**Author:** ![ahmadsayeed3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadsayeed3/32/72888_2.png) [@ahmadsayeed3](https://discuss.elastic.co/u/ahmadsayeed3)\
**Post date:** [September 17, 2020, 3:43pm UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/3 "2020-09-17T15:43:53Z")

</div>

Hi, Thanks for the quick response.

May I know where can I find Offset information.  
Screenshot:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/1/015bf5bc29d75c9d084c33ef7c3e3164a882f8fd.png)

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [September 17, 2020, 3:45pm UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/4 "2020-09-17T15:45:47Z")

</div>

I was interested in the timezone you're living, whether the offset to UTC is +12, thx

---

<div class="post-metadata">

**Author:** ![ahmadsayeed3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadsayeed3/32/72888_2.png) [@ahmadsayeed3](https://discuss.elastic.co/u/ahmadsayeed3)\
**Post date:** [September 17, 2020, 3:48pm UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/5 "2020-09-17T15:48:17Z")

</div>

Oh right.  
My timezone is India Standard Time ‎(UTC+5:30)‎.

---

<div class="post-metadata">

**Author:** ![ahmadsayeed3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadsayeed3/32/72888_2.png) [@ahmadsayeed3](https://discuss.elastic.co/u/ahmadsayeed3)\
**Post date:** [September 17, 2020, 3:57pm UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/6 "2020-09-17T15:57:12Z")

</div>

> [@ahmadsayeed3](#):
>
> return new Date().getTime()

How can I get it in UTC so I will get correct difference.

---

<div class="post-metadata">

**Author:** ![ahmadsayeed3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadsayeed3/32/72888_2.png) [@ahmadsayeed3](https://discuss.elastic.co/u/ahmadsayeed3)\
**Post date:** [September 21, 2020, 7:04am UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/7 "2020-09-21T07:04:11Z")

</div>

Hi matw, I am new to ELK, would appreciate if you please guide me on this?

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [September 21, 2020, 7:31am UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/8 "2020-09-21T07:31:26Z")

</div>

> [@ahmadsayeed3](#):
>
> getTime

Sure, sorry for the delay, so `new Date().getTime()` should return UTC. So I would check 2 things:

1. Is the server where this is executed, Elasticsearch is running in the right timezone?
2. Is the raw timestamp stored in the Elasticsearch record in UTC?

Pretty sure this is a timezone issue, so first you've to check, if all component's timezone is correct.

Best,  
Matthias

---

<div class="post-metadata">

**Author:** ![ahmadsayeed3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadsayeed3/32/72888_2.png) [@ahmadsayeed3](https://discuss.elastic.co/u/ahmadsayeed3)\
**Post date:** [October 6, 2020, 5:13am UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/9 "2020-10-06T05:13:32Z")

</div>

Strange behavior I have observed.  
When running in day it's giving correct difference.

When I run at night, it gives 12 hours difference. I checked and all set UTC format.

I don't know where should I look next.

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [October 6, 2020, 8:31am UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/10 "2020-10-06T08:31:47Z")

</div>

I'd suggest looking at the raw values of

> [@ahmadsayeed3](#):
>
> job\_start\_time

to find out when it this wrong difference is calculated. you say it's

**09/17/2020 02:07:35.379**

could it be is using the 12 hour clock instead of the 24 hour clock? this is just a wild guess, if e.g. it's wrong 0-12 and right 12-24 ...

---

<div class="post-metadata">

**Author:** ![ahmadsayeed3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadsayeed3/32/72888_2.png) [@ahmadsayeed3](https://discuss.elastic.co/u/ahmadsayeed3)\
**Post date:** [October 8, 2020, 7:46am UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/11 "2020-10-08T07:46:48Z")

</div>

Thanks matw.  
That solved. The problem was cause of 12 hour clock.  
24 hour clock working good.

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [October 8, 2020, 10:16am UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/12 "2020-10-08T10:16:22Z")

</div>

Happy I could help

---

<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 5, 2020, 10:16am UTC](https://discuss.elastic.co/t/scripted-field-getting-12-hours-date-time-difference/248999/13 "2020-11-05T10:16:32Z")

</div>

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