logo

NJP

ServiceNow GlideDateTime Format Made Easy: ServiceNow Hands-On Learning & Use Cases!

Import · Oct 18, 2023 · video

[Music] hi all and welcome back so in today's Tut we'll be focusing on very Niche topic okay which I think a lot of developers or I can say all the developers are facing that is with respect to date formatting date and time formatting into service time instance okay so as we know whenever we want to play around some date formatting into service now it's really difficult to play around that daytime apepi okay to get the response we want the reason behind this is like because we don't know the core of this particular apepi can how exactly we can use it out to tweak the things the way we want for an instance if we are importing data from certain third party application which which provides the date and time into a different format of but not in the format which is defined to the service now it's just it's really difficult for us to get it formatted into the service no format and then um insert it in the record why was if if if certain system integration system is asking data into certain format of which is not matching to the format of service now instance by default format then it's again a difficulty for us to retrieve the data get it formatted into a different way out and I've seen a lot of people writing on tons of line of script okay just to get it done but if you know the core of the foundation of this particular AP it's very easy to get get all the things sorted so without any wasting any further time okay let's dive into this tutorial okay and as you can see on the screen this is my personal development instance so first of all let's understand the foundation because I was talking about the foundation from last 10 minutes right so what exactly is what what I mean by the foundation of this particular AP so basically what happens every every API as you know service now is basically built on Java okay and every every API has something to do with the Java classes okay because they use out of the box JS and all those things out right so for the Glide dat time API the Java class which is used is simple date format class okay so as you can see on my instance if I go to the browser and if we U search for Java simple dat format class and if you go to the Oracle dogs okay now yeah Oracle docs um this is a class okay this so the main the main use of this particular class is to play around the format so simple dat format is a is a con class for formatting and passing data into local sensitive maners it allows for formatting dat text okay passing text text to date and normalization so we doesn't care about all those things the only thing which we care about how the pattern get signified over your okay so here you can see the abbreviations right stand for designator y stand for your small y okay capital Y is week year m is month in a year okay L the special one is e okay is whenever we give e it's it defines it into text okay like Tuesday you so kind of thing okay so we'll be using this particular abbreviations from this particular class and play around the DAT M apaa okay so let's jump to the instance and I will open the background script and let's go with one one example at a time okay so first of all for example variable like date time I hope I'll just so yeah I think this is better time go to new Glide dat time and we have we have a what we can say a current date for instance let's let's give it like this okay so what is the current date if someone can or let's take something 2016 DH 04 Das 02 and it will be 14 0 0 col 0 0 so this is the by default format okay with service now instance player out okay this is the by default for with service no except okay I'll just give gs. info one so then I will not mess up around things out now if we if you are getting um certain dat into a different format okay so if you want to Define it out so how we can Define it for example um let's let's not not play around first of all let's see how exactly it works so if I do gdd dot set display value and what I will do I'll give it like 215 1983 and now what what else so we'll get 20 colon 0 0 0 15 so uh this is the format which we are getting and now if you want to Define it into a format okay like from the document if I'm referencing to the doc so it will be like DD we are just defining which particular format so we'll be playing around this only so whenever you get some input string okay you have to Define in which particular format this particular input string is and service now by internally will convert into into a standard form I'm just showing it so why why why why because it's four digits right so I'm just matching it so 21 is DD five if I if I want to give five it's 0 five so it's mm then y y y y okay and then we will continue it's a in second right so how exactly it's it's HH colon mm colon SS okay let's verify it what exactly it's so D where is the small d d d is a d of okay capital Y is the is a small y we are looking for the small Y where the small y small Y is here okay in the similar we have kept the ledge it's R which is a number okay so you have to keep all this things in mind and if I go and simply do js. info J gdt do get value let's see what exactly so it should given into the default format which service now recognize it's already there in that format only oh I'm not able to see and but only run script say if I run this out it gave me the error what it's gdt not GST okay if I run this out so this is the format the biod defa format for this okay now let's move on to the example okay so that the things will get clear to you somewhat more so in the example two what I want to do if we have a Glide date Ty okay in in certain format okay we what we'll be taking we'll be defin planing a local time okay or let's do one thing okay what we'll be doing uh we will take certain string out okay which is into different format and now we want to pass it out into our service format so let's take this only I will I will just comment this out and I will just take this one X and we are we are getting the string into a different form for example GT Che not cheat do set value set display value and the format which you are getting from external system is without any it's two 15 02 04 okay and it will be 22 colon 30 col 44 something like this so here if you see okay if you see here what we can see that the it's like year 2015 the year 0 to maybe it's a month okay DD is a day okay and we are getting into this format uh minute second now we want to we can we can put it directly into the system right because it will get screwed up right so we have to convert it into service not base format so it's pretty easy to do that what do you have to just do you have to just Define which particular format this particular string is coming and it will internally will change it out to the default format okay so let's do that so we can see it's y y y y we got is 04 then mm for month I'm just referencing to document again I'm saying then DD DD is for day and then we have space hn mm SS so we have defined the format and now if we just get the display value it should convert it internally to service not defined for let's try it out so if I do gs. info and if I do gdt dot get display value right display value so let's see what thec see it has converted in 2015 02 04 isn't it amazing this is what we are looking for okay this is this is what we want now for instance let's let's go one more example if you want to if if certain system is asking for the date into a different format okay into a text format kind of so what we can do for example if I go um if I go with an example I will I will just comment this out okay and let's take this particular thing about if I take this out I will make it example three I hope it's getting clear to you how we are how we are changing the format now in the previous example what we saw that we are getting from external system we are getting into this particular format we are converted into service not defin format perfect right now if I go with another example okay where we have certain format out and we want want to give them into different format how we can do that so let's do that variable GD equal to new Glide dat time and now I will showcase what are the different ways to do that okay so first is GS doino I'll do and I will give the P the format which service now follows so I can do gdt dot get by by format is the function and in that we can give the format so the default service format is y y y y- mm TD and HH colon MF colon SS so this is the default format if I run this out so it it's giving me some error what that error is argument is missing oh I added it here okay and if I run this out now I undefined why it's coming because I again made a spell mistake again entity gtt Glide dat time it's it's let's with the Glide date okay okay perfect it's it's for the Glide date and now this is the general format okay of the service now instance now if you want to twak this out so we can do that we have to just Define the format in which we want the data so I just copy pasted it out P it out and now I will give some other format let's give the format which show you the clear so the format I want is something in the text format so what I can give I can give M M M M DD comma y y y y so it's month date month in uh what we can text then date in numeric then again here in numeric and then we have HH colon mm colon SS so we want in this format now so if I run this out you can see isn't it amazing we have just covered we have just changed the format into one sentence it's October 01 2023 14 51 20 the format in which we asked right let's look at the one more example around the same which example we should see so let's let's take one more example let's try for something more complicated with as as I've shown you the Glide dat okay let's try something with glide dat M API okay so what I will do I will do variable gtt1 equal to new Glide dat time and now assume that we are getting something in very difficult form so sou gdd do set display value and in that it's Friday which is a week date okay and August Friday August 18 20 what 18 like let's give again comma then 20117 then 8 z0 0 and it's a isn't amazing right now we have to just go to the docs and see how exactly we can we can we can format it out so let's jump with the doc so it's it was Friday right so Friday which is in text format so it's e okay so that's that's one thing that we have to Define it why we have to we have to format it using e then we have August then August where is August August so it should be capital M okay because you can see over here July we are getting right so it should be mmm kind of thing okay then we have dat which is DD where is d d small d right numeric because this numeric the same with the year which is numeric then again CES the point that there was there was what we can say time right which is in hours because we are playing with Am Pm now okay so it's denoted by key you can see small k r in 1 to 24 so it's a number okay and now again ISM so how exactly it's defined so it's using a isn't it easy if we look into the do okay let's do the same so I will give go with e then we have comma then space then mmm then space DD comma y y y y comma space Kore mm a so what exactly we are doing over here we are just giving them the inputs that see this is this is uh we are just giving them the input that this particular text which we are getting from the external system or anywhere right it's into this format now just go and convert it into the format which we are looking for perfect right so let's do that and how we can do that so we have to do GS dot do info and it will be td1 dot get display value and now it should convert into our format let's see and it gave us huge error okay probably I have screwed up something over here see quite go down down down up up up up up couldn't dispatch the Stag place for the following it's Friday August 18 just remove this space out 17 space key and it should always always keep in one because we are giving it it will pass the string as is right so if there are two spaces just mention the two spaces out there I missed one space that's why it was G me there and now we can see it got change okay this what to 2023 this 10th month 01 okay and whatever is coming out over there as aning right okay let's try a few more things out so let's look into the next example I don't know how many examples I have covered okay because I'm that excited so let's play around gtt2 sorry variable gtt2 equal to new Glide date time and we'll be using G date function and then let's and tweak around the dates so we'll go get date and now let's print it out into two different formats okay which which will cover some text okay so js. info the first one let's have Triple E what's the def Triple A because in the previous example you have said triple so what the defle A and four time e or one time I will show you that so it's e we can go sorry D D2 dot get by for M there a function previously we were not getting it and I will e e space d d space mm and let's control C I'll just paste it out and I will add one more e and one more M and now see the difference okay I will comment all the things above if I run this out so this is the difference so if you have proved trip it gave a short form for the particular week day or for particular month like sun 01 October and Sun day 01 October you can see this this difference right it's it's reading I really like this out because I I messed up around this thing like hell okay now let's take example now let's T at some use case okay which we face into our day-to-day life you must be thinking what and that use Cas is when we want to find whether that particular day is a weekend or weekday and so on right so let's define a function and see how exactly so I will call I will make it a self invoking function so function is week day and I will I will Define the variable is week day variable is weak and so we have to find whether that particular current date is weekday or weekend so what you can do it's variable gdt equal to new Glide dat and then now you have seen this Triple E magic of right so we will just find it out and see it matches certain string or not okay how so we can do variable D equal to D Dot and get by format and we just want the day okay the week day and it should be completely Sten if you want you can use the short form also triple okay and if fre code for right just triple this particular thing we are using okay and to make sure that we are getting it properly we'll just try to make it string how we can do that in shortcuts perfect and now we can simply add switch switch what day and now we can has case around the same if case if we get Saturday or let's if it get Sunday or if we get Saturday then we can set this particular flag that it's weekend or week so is why it's start coming out there as day okay equal to false is we equal to True okay perfect and we'll break it out and by default it should be daa I want to make it X false and now let's to TS do info and a day and the similar thing we can go for this house and let make invoking so if you run this out now again it gave the error 289 what have dun H it's function and if I run this out so g g the and show I watch oh

View original source

https://www.youtube.com/watch?v=rEwOktAvtcA