logo

NJP

Learn MORE JavaScript on the ServiceNow Platform: Lesson 10 - GlideDateTime

Import · Dec 12, 2023 · video

Welcome back to our series Learn More
JavaScript on the ServiceNow platform. We've come to a feature
that's crucial for any system dealing with dates and times
GlideDateTime. It's a ServiceNow class that provides methods for performing operations
on dates and times. Fasten your seatbelts. We're about to make a time travel
look easy. Okay. Glide take time allows us to handle
the complexities of things like date and time calculations, timezone, conversions,
and formatting of dates and times. It's especially useful
for ensuring consistency when we're dealing with time zones
for different users. Okay, let's
go ahead and just jump right in. So if I wanted to create a new glide
GlideDateTime object, it's similar
to other ways that we have with classes. We want to create a variable
so we're going to call our gdtNow and then we want to create
a new GlideDateTime object, and that's how you instantiate
a new GlideDateTime. So let's go ahead and right away
just print this to the logs and see what this looks like. Yeah, it's a current date and time
according to the system, so it might not be your local time zone,
but it's according to the system. So that's very helpful
because if you're using just blank GlideDateTime now you know that
it's operating based off of the system, your instance’s time zone, not necessarily
your time zone or a particular user’s. So it's a little bit
more universal at this point. Now we can start manipulating
the GlideDateTime object to maybe show some time in the future
or some dates in the past. So for the example of let's manipulate the dates of it,
that's as simple as saying gdtNow and we're going to say add days
according to the local time. So we can even make it add days
according to my time zone, the person that's running the script. So if I add five days to it, let's
go ahead and show what that looks like. We're going to say gdtNow, get local dates and we can even display it
in a different format if we want. So getByFormat() and we give it
a specific format that we want to give it. So in this one, I don't care
about the time, I just want the date. let me run the script
and show you what it looks like. So it shows as the default year
and then month and then date. But if we wanted to maybe change it
to month, date, year, there we go. So displays it
the way I asked it to display it. it's not only displayed
in the format that I want, but it's also changed the date, according
to what I put it in and in local time. Okay, so now let's go ahead and manipulate the time
part of it. The time part of
it is a little bit different. For the date you can to addDaysLocalTime(),
addWeeksLocalTime(), addMonthsLocalTime() but for time, just the time part of it,
everything is done via gdtNow.add(). That's it. Just add, not add minutes
local time add hours. It’s just add and from here
it's all by milliseconds. So if you know exactly
how many milliseconds you want to manipulate it,
then you're golden. So I want to do 30 minutes in the past. Now even we can even do in the past,
not even just the future, but in the past, 30 minutes ago. And I know this is 1 million and 800000 milliseconds,
so I'm going to add that for there. And then let's go ahead and print
that and say, gdtNow.getLocalTime(), not date, getLocalTime()
and getByFormat(), and we'll do a pretty traditional format of hours
and then minutes and seconds and go ahead and print that and you'll see it was right now
it's actually I'm doing this at nighttime. So. 10:09 p.m. and 2139 that's 9:39 p.m.. So 30 minutes ago, perfect. That worked. Okay, the next part of our lesson I'm going to show you how to compare
two different dates, it’s very useful. We have a lot of dates... GlideDateTime fields inside ServiceNow. for example, say that we have a field
that is a date time and we want to see if this other date in time is before
or after it and based off of if it's before or after
we want to run a different kind of script. So the way we would do that is we would
create the first object of GlideDateTime. We're going to say,
we're going to call this one gdtStart. We're going to say new GlideDateTime. And instead of just instantiating
the object without a parameter. So see how this parentheses is blank. We can actually type something in here,
or we can put a variable in here to say, Hey,
right away, we don't want this GlideDateTime
object to be based off of a current time. We want to based off of stored time or the specific time. So I copy and pasteed this one,
but I put January 1st of 2023 at 8:30 a.m.. And so now instead of saying, it's the current date, it's immediately
thinking that object is that date. And very similarly we're going to say
gdtEnd = new GlideDateTime and we're going to give it will change the time of this one to, let's say 45 minutes later. And so now to check it, to check
if this is if this start date is properly
before the end date, we'll go ahead
and say if gdtStart is before gdtEnd print the statement saying the start gdt is before the end gdt. So if we print this, it should print the
statement because it is true. There you go. Now let's change it to say
this one's in the let's put this one ahead of the end time
and we should not get anything Yup. The logged in imprint
because that if statement wasn't true. Yep. So that's how you compare
two different DateTimes. And the last thing I wanted to show you
all today is something very important. And honestly, a lot of developers
know that this is like the, the most frustrating part of dealing with anything about date
and time is timezone conversion. We all have to deal with it eventually
and it can be frustrating. And there's a lot of trial and error,
but you have to dip your feet in some time and let's go ahead and show you a little bit of one of the ways
to deal with timezone conversions. And so let's create a new gdt object. We're going to call this gdtEst. So for Eastern Time
Zone, we're going to go ahead and just create a new glide time object. And then another way
to set the time for this object. We could easily put something
inside of that parentheses like I did in the last part of the lesson. But another way to do
it is saying SetValue(). So I copied and pasted
one that says noon at January 1st on 2023. So that GlideDateTime
object is set to that time. And now let's pretend that that time I gave it is the UTC time zone and what we're looking for is Eastern Time
zone. To do that. We're going to say dot convert time zone and we give it the starting time zone. UTC with the ending time zone. So this would be America slash York. So you're going to you can look it up
to see what all the normalized naming mean for all these time zones
and then that's how you format it. So let's
go ahead and see what that looks like. This is the time in
and we're going to go ahead and print that by saying getDisplayValue(). Let's see what it looks like. There you go, so apparently noon in UTC
is the same as 4 a.m. in Eastern time zone. Cool, and those are some of the basic ways
to play with GlideDateTime. There's a lot more methods
within the GlideDateTime class, so you can look it up, but some of those
are the more commonly used ones. Cool, I'll see you in the next one.

View original source

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