Scripting around dates and time in ServiceNow has caused every ServiceNow Engineer some pain at a certain point.
Here’s how to get the current date and time in ServiceNow.
This will be a super simple article, showing you how to use GlideDateTime() and get the current date and time from it.
When you’re using the GlideDateTime() object, you’re limited to ServiceNow’s methods to retrieving date time information, which a lot of people complain about.
To just print the current date and time in a single method, use:
/* GlideDateTime */ /* Get the current time */ var gdt = new GlideDateTime(); var date = gdt.getDate(); var time = gdt.getTime(); gs.print("date: " + date); gs.print("time: " + time); gs.print("gs.nowDateTime(): " + gs.nowDateTime());
When you run the following script in a background script in ServiceNow, you will get the following output:
Give the above example a try in a background script.
This will work in any server side script, so a business rule or script include.
The above example will not work in any client side scripting.