# Convert current time into minutes using painless

**URL:** <https://discuss.elastic.co/t/convert-current-time-into-minutes-using-painless/262991>\
**Category:** Kibana\
**Created:** [February 2, 2021, 1:52pm UTC](https://discuss.elastic.co/t/convert-current-time-into-minutes-using-painless/262991 "2021-02-02T13:52:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gokul6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokul6/32/84049_2.png) [@Gokul6](https://discuss.elastic.co/u/Gokul6)\
**Post date:** [February 2, 2021, 1:52pm UTC](https://discuss.elastic.co/t/convert-current-time-into-minutes-using-painless/262991/1 "2021-02-02T13:52:59Z")

</div>

**def now = new Date().getTime();**  
**return now;**  
in kibana scripted fields i am getting current date and time using the above code. the output of the above code is **Feb 2, 2021 @ 16:47:24.807**

i want to convert this time entirely into minutes. how to do this ?? please help 🙂

---

<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:** [February 2, 2021, 2:08pm UTC](https://discuss.elastic.co/t/convert-current-time-into-minutes-using-painless/262991/2 "2021-02-02T14:08:23Z")

</div>

Hi

You mean you wanna convert this time to display just the minutes or you want to display the number of minutes since 1.1.1970 aka unix or epoch times?

Thx & best,  
Matthias

---

<div class="post-metadata">

**Author:** ![Gokul6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokul6/32/84049_2.png) [@Gokul6](https://discuss.elastic.co/u/Gokul6)\
**Post date:** [February 2, 2021, 2:13pm UTC](https://discuss.elastic.co/t/convert-current-time-into-minutes-using-painless/262991/3 "2021-02-02T14:13:27Z")

</div>

yes sir i want to convert the current time to display just the minutes @matw

---

<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:** [February 2, 2021, 2:57pm UTC](https://discuss.elastic.co/t/convert-current-time-into-minutes-using-painless/262991/4 "2021-02-02T14:57:03Z")

</div>

You can try this way:

```
def now = LocalDateTime.ofInstant(Instant.ofEpochMilli(new Date().getTime()), ZoneId.of('Z'));
return now.getMinute();
```

---

<div class="post-metadata">

**Author:** ![Gokul6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokul6/32/84049_2.png) [@Gokul6](https://discuss.elastic.co/u/Gokul6)\
**Post date:** [February 2, 2021, 3:10pm UTC](https://discuss.elastic.co/t/convert-current-time-into-minutes-using-painless/262991/6 "2021-02-02T15:10:30Z")

</div>

thank you for your time and help @matw i am able to get the minute of current hour using the code that you gave.

similarly i am able to get the total hours elapsed for the day using  
def now = LocalDateTime.ofInstant(Instant.ofEpochMilli(new Date().getTime()), ZoneId.of('Z'));  
return now.**getHour()**;

i will multiply this hour value with 60 and get the entire hours elapsed in minutes.

---

<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:** [February 2, 2021, 3:17pm UTC](https://discuss.elastic.co/t/convert-current-time-into-minutes-using-painless/262991/7 "2021-02-02T15:17:10Z")

</div>

Asked for a simpler version in our painless expert channel, there ain't no, but I wanna give the advise they gave:

> you really should pass in a datetime or minutes through parameters as this will just pick up the time when the script happens to run. It will not be consistent.

Best,  
Matthias

---

<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:** [March 2, 2021, 3:18pm UTC](https://discuss.elastic.co/t/convert-current-time-into-minutes-using-painless/262991/8 "2021-03-02T15:18:10Z")

</div>

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