The joy of getTime() & getByFormat()
G?ran Lundqvist - ServiceNow Witch Doctor
Expert Training Programs - Principal Delivery Manager | ServiceNow MVP[2017 - 2021] | Author
I looked at the result I got "*** Script: [DEBUG] 06:00:00" and then I looked at the code:
var gdt = new GlideDateTime("2014-08-31 18:00:00");
var gt = gdt.getTime();
gs.debug(gt.getByFormat('hh:mm:ss'));
Why didn't I get 18:00:00 in the result? I couldn't see any parameter to tell the code to go 24H instead of having some wicked way of AM/PM. I couldn't get it off my mind even if I just thought "okay, let's do some more coding to get out the time from the variable instead". So after asking around, there was a very simple way. Just replace hh with HH and I got what I wanted. I couldn't find any documentation about that right there and perhaps everyone else knows about it :P. But if I'm not alone, just have the code like this and you will be fine:
var gdt = new GlideDateTime("2014-08-31 18:00:00");
var gt = gdt.getTime();
gs.debug(gt.getByFormat('HH:mm:ss'));
And if you are too lazy like me, I got the code snippet on the GitHub as well: https://github.com/goranlundqvist/YouTube
Take care,
//G?ran
ServiceNow Architect
5 年When I faced similar issue, I had to convert timezone to fetch the correct time. Happy to know this functionality, simple and easy!! Lot of hidden stuff in there!!
ServiceNow Certified Technical Architect (CTA) | Senior ServiceNow Architect
5 年If I do HH:MM:ss it gives 18:01:00 instead of 18:00:00!
Principal Consultant at Slalom
5 年Nice find! I would have been stuck on that one for a while... :)
Principal Consultant at Infosys
5 年Good finding! Looks like more details on Time format can be found here .. https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/administer/time/reference/r_TimeFormat.html
Sr. Technical Consultant
5 年Thanks for the info..I was stuck in similar issue and your post saved lots of my time.