ServiceNow GlideDateTime() – Get The Current Date And Time

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 …

get the current date and time

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:

gs.nowDateTime();

Alternative to the GlideDateTime() class, you can use the JavaScript Date() object, which has more methods to retrieve helpful information from.

/* 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.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x