GlideDateTime

Mark Miller
A free video tutorial from Mark Miller
ServiceNow Guru
4.4 instructor rating • 5 courses • 38,903 students

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 October 2019

  • Develop on and customize for the ServiceNow platform
  • The ServiceNow APIs
  • When & where to script in ServiceNow
English [Auto] Hello and welcome to Section 8 where we'll explore other API is within service now. We'll start the section by first looking at the glide date time 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 SML document to API which might be used for things like Web services. Then we'll quickly take a look at a list of other API within the service platform and then we'll cover some of the undocumented API is and I'll show you a few tips and tricks on how to find out more about these undocumented API is then we'll take a look at script includes and even creating our own API using script includes we'll end this section with a section recap. So let's get started the service now docs explain the glide date time API as the scoped glide date time class provides methods for performing operations on glide date time objects such as instantiating glide date time objects or working with Glide date time fields as a developer. We use the glide date time API any time we need to do date time operations or format date times or convert between date and time formats. This is a serious site API and cannot be accessed on the client side. This slide shows us three ways to instantiate a glide date time object. The first way on line 2 is we just simply do new glide date time without any arguments. This will create a new glide date time object with the current system time and assign that to the GDP variable. The second option on line 5 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 if we pass in 2017 0 5 25 space 12 to 13. Finally the third option shows a variable with that string already assigned to it and then we're just simply passing that variable as an argument so lines 5 and 9 the day time are essentially identical. There is 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 the glide date time object add months UTC add seconds add weeks add years etc.. We have before and after methods that will compare to 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 date method which will return the date stored in the date time object we had day of month UTC get local time get month to UTC get time get week of year UTC year UTC. We haven't is valid method which will test if the glide date time object is valid. We have honor before honor 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 1. I have a new glide date time object assigned to the variable star which represents January 1st 2017 at 9 am online too. I have another variable end which is assigned the glide date time that represents January 7th at 8 am of 2017. Then on line 4 I create a new variable difference and assign this the value returned to us from Glyde date time subtract or repasts into arguments the start and the end time. Then all 9:5 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 in to service now and take a look at the API. So for the purposes of this API we'll be using our handy dandy background scripts where will execute our code. We'll start things off by recreating what we saw in an earlier slide. So we'll create three G.D. variables Judy TGT 2 and Judy T-3 and assign these the values of Arnstein sheet glide date time object finally we'll go ahead and print each one of these to the screen so we copy and paste this code in a background script and click run we can see that we do in fact have three like eight times that come back to the code. I want to quickly put in the type of javascript keyword which will now print that 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 that represents our glide date time object. All right. Now let's take a look at the Days UTC. So we'll keep line one where we just create a new glide date time object of the current date and then on line 2 will go ahead and log that. So we'll give it a value of now plus the GDP and then on line 3 will do GDP add days UTC or we pass an integer through 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 at month's UTC. So for this will simply change the method name and leave it as 3 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 format that so this will test two dates and see if the second date is before the first so online one will keep our new date and then on line three will create a new gleich daytime and we'll do a guest that print is G.T. before G.T. to and then do GED. Before we pass in the second date time object as its argument. So I'll copy and paste that and we get c that we get false here. And this is actually the case because the glide date time object is evaluating these based on the second and because the system is processing these within the same second. DDT isn't technically before DDT too. So to fix this we'll go ahead and throw a string value in our first glide date time 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 compare to method so the compare two 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 1 if the initial date is before the compare date and we get a negative one. If the initial date is after the compare date. So let's take a look. We can see that we get a negative one here because DDT 2 comes after DDT if we reverse then we'll get a 1 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 glaive 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 glide date time object Ged's 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 the value returned to us from glide date time subtract GDP GDP to 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 students are not evaluated with this method.