Convert current time into minutes using painless

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 :slightly_smiling_face:

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

1 Like

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

You can try this way:

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

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.

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

1 Like

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