GlideDateTime - get hours, minutes and seconds

davide_fais
Tera Expert

Hi all

How Can I extract hour, minute and seconds from DataTime field?

for example, I'm trying to write this business rule

var gdt = new GlideDateTime ("2017-03-31 15:13:43");

year = gdt.getYearUTC();

month = gdt.getMonthUTC();

day = gdt.getDayOfMonthUTC();

hour =

minute =

second =

Thanks,

Best Regards.

Davide.

1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi Davide,


Interesting we don't seem to have any methods available to easily get a handle to these values, however if you want a quick answer (considering its almost close of business on a Friday), you could use the getValue() method.


This returns a String of the date and time value stored by the GlideDateTime object in the internal format, yyyy-MM-dd HH:mm:ss, and importantly the system time zone, UTC by default.


From this you could use the String methods available to get a handle to these values. Foe example the split method and/or substring method etc.


Not the cleanest of solutions but I'll take a look later for you.



Please mark this response as correct and helpful via your post link (GlideDateTime - get hours, minutes and seconds ) to help others with the same question.



Thanks,


Robbie


View solution in original post

8 REPLIES 8

Robbie
Kilo Patron
Kilo Patron

Hi Davide,


Interesting we don't seem to have any methods available to easily get a handle to these values, however if you want a quick answer (considering its almost close of business on a Friday), you could use the getValue() method.


This returns a String of the date and time value stored by the GlideDateTime object in the internal format, yyyy-MM-dd HH:mm:ss, and importantly the system time zone, UTC by default.


From this you could use the String methods available to get a handle to these values. Foe example the split method and/or substring method etc.


Not the cleanest of solutions but I'll take a look later for you.



Please mark this response as correct and helpful via your post link (GlideDateTime - get hours, minutes and seconds ) to help others with the same question.



Thanks,


Robbie


Robbie
Kilo Patron
Kilo Patron

Hi Davide,


How did you go?



Please mark this response as correct and helpful via your post link (GlideDateTime - get hours, minutes and seconds ) to help others with the same question.



Thanks,


Robbie


Ganesh Bhat
ServiceNow Employee
ServiceNow Employee

There is a straightforward way to get hour, mins and second from any given date.

GlideDateTime provide getTime method, which returns GlideTime object.

GlideTime has api to return you all the time related data.

 

var gdt = new GlideDateTime("1970-01-01 06:02:31");
gt = gdt.getTime();
gs.info('hours is:'+gt.getHourUTC())
gs.info('minutes is:'+gt. getMinutesUTC())
gs.info('seconds is:'+gt. getSeconds())

 

This is the recommended way to obtain time.

 

Here are the api for reference

https://developer.servicenow.com/dev.do#!/reference/api/madrid/server/r_SGT-getSeconds

 

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/r_GDT-GlideDateTime

I tried this in a business rule and getting following error:

Function getTime is not allowed in scope global

Do you know why?

~Thanks