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,


Is this a display issue such as converting a duration value such as '23:24:41' to something that would read 23 hours, 24 minutes and 41 seconds ? Or are you wanting to get a seperate 'hande' to these values?



Have you tried leveraging the javaScript Date object? The below should help you out...


(You can run this as background script to test if you want to play - assuming youre in Dev)



var currentTime = new Date();


var year = currentTime.getFullYear();


var month = currentTime.getMonth() + 1;


var day = currentTime.getDate();


var hours = currentTime.getHours();


var minutes = currentTime.getMinutes();


var seconds = currentTime.getSeconds();



gs.print('currentTime :' + currentTime);


gs.print('hours :' + hours);


gs.print('minutes :' + minutes);


gs.print('seconds :' + seconds);



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


my code is this, but dont work extrating hour,minutes and second



var gdt = new GlideDateTime(current.sys_created_on);
gr.u_opened = current.sys_created_on;
gr.u_opened_year = gdt.getYearUTC();
gr.u_opened_month = gdt.getMonthUTC();
gr.u_opened_day = gdt.getDayOfMonthUTC();
gr.u_opened_hour = gdt.getHours();
gr.u_opened_minute = gdt.getMinutes();
gr.u_opened_second = gdt.getSeconds();

Hi Davide,


Sorry, I misled you a little. The getHours() etc methods are from the Date object, not the GlideDateTime object.


Let me take a look for you.



Thanks,


Robbie


Shivam10
Tera Contributor

Please note here timing are coming from broswer time not from the servicenow timing