- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-08-2022 03:19 AM
Hello ServiceNow Ninjas,
Javascript & ServiceNow scripting run hand in hand. We often use both in a mixed way such as using javascript API within Glide APIs or vice versa. And when it comes to dates I have faced a lot of confusion.
In this article, I am providing a few basics of Javascript Dates that help in identifying, managing & using javascript date objects in a better way.
All below code snippets are tested in the background script.
1. Declare a new date object:-
var d = new Date();
gs.print(d);
*** Script: Thu Sep 08 2022 03:29:47 GMT-0700 (PDT)
2. You can initialize the date as well:-
var d = new Date(2018, 11, 24, 10, 33, 30, 0);
gs.print(d);
*** Script: Mon Dec 24 2018 10:33:30 GMT-0800 (PST)
3. A date object can be created using "date strings" as well:-
var d = new Date("October 13, 2014 11:13:00");
gs.print(d);
*** Script: Mon Oct 13 2014 11:13:00 GMT-0700 (PDT)
4. How to parse date?
If you have a valid date string, you can use the Date. parse() method to convert it to milliseconds. Then milliseconds can be converted into a date object.
var d1 = Date.parse("March 21, 2012");
gs.print("Date1 - "+d1);
var d2 = new Date(d1);
gs.print("Date2 - "+d2);
*** Script: Date1 - 1332313200000
*** Script: Date2 - Wed Mar 21 2012 00:00:00 GMT-0700 (PDT)
5. Use the following methods to extract information from the date object:-
Method | Description |
---|---|
getFullYear() | Get the year as a four-digit number (yyyy) |
getMonth() | Get the month as a number (0-11) |
getDate() | Get the day as a number (1-31) |
getHours() | Get the hour (0-23) |
getMinutes() | Get the minute (0-59) |
getSeconds() | Get the second (0-59) |
getMilliseconds() | Get the millisecond (0-999) |
getTime() | Get the time (milliseconds since January 1, 1970) |
getDay() | Get the weekday as a number (0-6) |
Date.now() | Get the time. ECMAScript 5. |
var d = new Date();
gs.print(d.getFullYear());
gs.print(d.getMonth());
gs.print(d.getDate());
gs.print(d.getHours());
*** Script: 2022
*** Script: 8
*** Script: 8
*** Script: 3
I hope this will help you in getting an edge over Javascript date objects and functions.
---------------------------------------------------------------------------------------------------------------------
I am providing amazing content on my YouTube channel & personal blog. Requesting you to go through it and share your valuable feedback.
YouTube Channel - ServiceNow 911
My blog - ServiceNow Spectaculars
Linked Group - ServiceNow Spectaculars
- 13,509 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
// Create a GlideDateTime object for the current time (in UTC by default)
var currentTime = new GlideDateTime();
// Convert the current time to the 'America/New_York' timezone
var estTime = new GlideDateTime(currentTime.getDisplayValue());
var timeZone = new GlideTimeZone();
estTime = timeZone.convertTimeZone(estTime, 'America/New_York');
// Create a GlideDateTime object for 4:30 PM today in EST
var fourThirtyPmToday = new GlideDateTime();
fourThirtyPmToday.setDisplayValue(estTime.getDate().getByFormat('yyyy-MM-dd') + ' 16:30:00');
fourThirtyPmToday = timeZone.convertTimeZone(fourThirtyPmToday, 'America/New_York');
// Create a GlideDateTime object for 11:00 PM today in EST
var elevenPmToday = new GlideDateTime();
elevenPmToday.setDisplayValue(estTime.getDate().getByFormat('yyyy-MM-dd') + ' 23:00:00');
elevenPmToday = timeZone.convertTimeZone(elevenPmToday, 'America/New_York');
// Check if the current time is between 4:30 PM and 11:00 PM EST
if (estTime.after(fourThirtyPmToday) && estTime.before(elevenPmToday)) {
gs.info("True: The current time is between 4:30 PM and 11:00 PM EST.");
return true;
} else {
gs.info("False: The current time is not between 4:30 PM and 11⬤