Learn MORE JavaScript on the ServiceNow Platform: Lesson 5 - Date and Time
Hello fellow scripters. Welcome to another episode of Learn
More JavaScript on the ServiceNow platform today we are learning about JavaScript,
some date and time objects, managing date
and time is a common task in programing and JavaScript provides us
with a date object to make it easy. So let's not waste
another second and dive right in. Okay. Sorry. The JavaScript date
object represents a moment in time. It can be used to get the current date
and time, perform some date manipulations and convert dates and times to a string. Remember the date
object always stores dates in UTC time and displays them in the local time
zone of the browser or server. That's a fun little background
fact for you. So let's see what kind of cool things
we could do with the date and time object. So I'm going to go ahead and create a new Date object and then let's start
playing around with it. I can find out the current date and time, let's say “current date and time” by just simply calling it. So let's see what happens when I run that. And now, like I said, it stores it as UTC, but my browser is in Pacific Time,
so it displays it as. Wednesday, November 22nd, 2023. At 12:48. Is it really 12:48? Man,
I need to eat lunch. and displays it in my timezone. The other things we can do with it is stuff like let's say, what is the year? “The year is” now. So what we see
we've stored date inside now so we can say now.getFullYear(). We could say “the month
is” same, similar way. Oops, let me put a plust sign there. now.getMonth() and we could say “the day is” now.getDate(). Let's run the script You'll
see the years 2023, the month is ten. We'll come back to that and the day is 22. So as of the recording of this video,
it is November 22nd, 2023, and so the month is off. And why is that? And that's because months are zero
indexed inside of this object. So if you want to display it properly, you have to always make sure to go
right here and make sure you say plus one. And okay,
now I'll show the gotcha here too. So I'll run the script
and it's going to say 10-1. 101. We got to remember to take that
that part of the code and put it in its own parentheses so that it treats
as integers instead of strings. There you go. Now it's proper. Now, if I wanted to say the year, month
and day, I can do it with the date
object inside JavaScript. So there's that. Other things
that we could do. we can also set
specific parts of the date. So let's try that right now. We'll get now.setFullYear(). Let's set it to 2020 instead
and then let's see what happens with that year inside of the function
“changed year”. And now.getFullYear() and let's run that. You can see the original one says 2023. Because when we created that date
object, it instantiated based off of what it is
currently, which is for me 2023. But then I set it to 2020
and then called it again. And now you can see it thinks it's 2020. And finally,
the last thing I wanted to show you you can do with
this is take that local date string and convert it to readable strings. So if I say “local date string” and now.toLocaleDate(), locale date string and call it. You can see it made it prettier. November 22nd, 2020. It still retained that
new date of the year that I set, but it's a much more readable format. To look up other ways
you can play with the date object and all the different methods that are
available to you within the date object. I honestly will search for it on internet if I I'm looking for a specific use case or a specific method
that might be helpful to me. Otherwise,
here are a few of the more common ones. Thanks!
https://www.youtube.com/watch?v=suYAK4j8zmc