Udemy

GlideDateTime

A free video tutorial from Mark Miller
ServiceNow Guru
Rating: 4.5 out of 5Instructor rating
5 courses
136,106 students
GlideDateTime

Lecture description

GlideDateTime contains many helper functions for dealing with dates and times in ServiceNow. In this video, we'll learn about the popular API.

Learn more from the full course

The Complete ServiceNow Developer Course

Learn how to develop on the ServiceNow platform!

08:41:44 of on-demand video • Updated July 2020

Develop on and customize for the ServiceNow platform
The ServiceNow APIs
When & where to script in ServiceNow
English [Auto]
Hello and welcome to Section eight, where we'll explore other APIs within ServiceNow. We'll start this section by first looking at the Glide DateTime API. We'll go over some of the popular methods and then take a look at a demo. Then we'll transition into the Glide Element API. We're already familiar with some of these methods due to Glide record, but we'll take a deeper look at these. Next is the XML document two API, which might be used for things like web services. Then we'll quickly take a look at a list of other APIs within the ServiceNow platform. And then we'll cover some of the undocumented APIs. And I'll show you a few tips and tricks on how to find out more about these undocumented APIs. Then we'll take a look at scripting clues and even creating our own using script includes. We'll end this section with a section recap. So let's get started. The ServiceNow docs explain the glide date time as the scoped glide date Time class provides methods for performing operations on Glide DateTime objects such as instantiating Glide, DateTime objects, or working with Glide DateTime fields. As a developer, you'll use the Glide DateTime API. Anytime you need to do datetime operations or format date times or convert between date and time formats. This is a server side API and cannot be accessed on the client side. This slide shows us three ways to instantiate a glide DateTime object. The first way on line two is we just simply do new glide datetime without any arguments. This will create a new glide datetime object with the current system time and assign that to the variable. The second option on line five is we can actually pass in a string value of the date time. We would like the glide date time object to be representing of. So here we can see we pass in 2017 zero 525 space 1202 30. Finally, the third option shows a variable with that string already assigned to it. And then we're just simply passing in that variable as an argument. So lines five and nine, the glide datetime are essentially identical. There's just a different method of passing in that string value. Here's a list of some of the common glide date time methods. This doesn't represent all of them, but just some of the more commonly used methods. So we have an Add method that adds milliseconds to a glide date time object. We have Add days UTC, which adds days to the glide datetime object. Add months UTC. Add seconds. Add weeks. Add years etcetera. We have before and after methods that will compare to glide date times to each other. We also have the compare to method. We have an equals which will compare to date times and return true if they are equal to each other. We have a getdate method which will return the date stored in the datetime object. We have git day of month UTC, get local time, get month UTC, get time, get week of year UTC get year UTC. We have an Is valid method which will test if the glide datetime object is valid. We have on or before or on or after methods. And then we also have a subtract method which will take a deeper look at here in just a second. All right. So the subtract method will return the difference in days, minutes and or seconds between two dates. So on line one, I have a new glide date time object assigned to the variable start, which represents January 1st, 2017 at 9 a.m. on line two, I have another variable end which is assigned the glide date time that represents January 7th at 8 a.m. of 2017. Then on line four, I create a new variable difference and assign this The value returned to us from glide date time dot subtract where we pass in two arguments the start and the end time. Then on line five will simply print the display value of this difference. So here we can see that the difference between the start and end dates is five days and 23 hours. All right, enough slides. Now let's actually jump into ServiceNow and take a look at the API. So for the purposes of this API, we'll be using our handy dandy background scripts where we'll execute our code. We'll start things off by recreating what we saw in an earlier slide. So we'll create three GDP variables. GDP, Gt2 and GT3 and assign these the values. Of our instantiated glide DateTime object. Finally, we'll go ahead and print each one of these to the screen. So if we copy and paste this code in a background script and click run. We can see that we do, in fact, have three glide eight times. If I come back to the code, I'm going to quickly put in the type of JavaScript keyword which will now print the type of value this is. So just to show you that this is not a string and it's actually an object in JavaScript, we'll go ahead and do type of all three variables and we can see that we get object. So thatrillionepresents our glide date time object. All right. Now let's take a look at the ad days UTC method. So we'll keep line one where we just create a new glide datetime object of the current date. And then on line two, we'll go ahead and log that. So we'll give it a value of now, plus the GDP. And then on line three we'll do GDP dot ad days UTC where we pass in an integer three representing the number of days we'd like to add. Now we'll go ahead and print this to the screen. So if we copy and paste this code, we can see that the new value represents three days in the future. All right, now let's quickly look at AD months UTC. So for this we'll simply change the method name and leave it as three. Copy and paste that code and we can see that it's three months in the future now. And we'll also do the same for ad years UTC. So we can see that this is three years in the future. Now let's take a look at the before method. So this will test two dates and see if the second date is before the first. So on line one, we'll keep our new date. And then on line three, we'll create a new glide. DateTime. And we'll do a sprint is GDP before GDP to and then do GDP before where we pass in the second glide date time object as its argument. So we'll copy and paste that and we can see that we get false here. And this is actually the case because the Glide DateTime object is evaluating these based on the second and because the system is processing these within the same second GT isn't technically before GT two. So to fix this, we'll go ahead and throw a string value in our first glide datetime that we know is in the past. And test this again. And we do, in fact, see true. All right, Now let's use the Compareto method. So the compare to is similar to the before or after, except instead of getting a boolean value back, we get a zero. If the dates are equal, we get a one if the initial date is before the compared date and we get a negative one if the initial date is after the compared date. So let's take a look. We can see that we get a negative one here. Because GDP two comes after GDP. If we reverse them. We'll get a one. And then if we make them both equal to each other, you can see that we get a zero. All right. Now let's take a look at the subtract method. So for this, we'll go ahead and pass in a string value to our first date time that we know is in the past. We'll go ahead and print that to the screen and then we'll create a new datetime object to. With the current time and we'll go ahead and print that. And then finally we'll create a new variable difference and assign it the value returned to us from datetime. Dot Subtract. GDP. GDP two. Finally, we'll go ahead and print this to the screen. So you can see that there is a three minute difference between the two. It's important to note that seconds are not evaluated with this method.