sabell2012
Mega Sage
Mega Sage

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

 

DIFFICULTY LEVEL:    INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.


So how do you compare two times? Strings won't work correctly (i.e. "04:00:00" is not necessarily less than "06:00:00"). The following code demonstrates how to manipulate a string time into a GlideTime, and how to extract the time component of a GlideDateTime to a GlideTime object.

 

I will also be demonstrating a couple of techniques that build slightly from my previous CodeSnippets DateTime article: link.

 

Try it out in Scripts - Background! I put plenty of comments into the code explaining what was happening and why. Play around with the start date and see what happens. Study on the dayOfWeek variable and see if you can think of a way that might be converted into a JavaScript switch. All sorts of possibilities!

 

var start = "2015-08-05 0:00:00";

// Same as the last example.  We are getting the local date time from the GMT
var startDate = new GlideDateTime(start);
var offSet = new GlideTime(startDate.getTZOffset() * -1);
startDate.add(offSet);

var dayOfWeek = startDate.getDayOfWeek();

// You can get the amount of time between a date and a day of the week.
var duration = new GlideDuration();
var span = startDate.getSpanTime(dayOfWeek);
duration.setValue(span);

// Another thing: GlideDuration inherits from GlideDateTime.
var myTime = duration.getTime();  // you don't have to string split the date time, this method retrieves the time from a GlideDateTime
var checkTime = new GlideTime();  // set up the GlideTime object for later use

// the start of the week is Monday, not Sunday.  Sunday evaluates to 7.  Monday to 1.
if (dayOfWeek == 3) {  
    gs.info('so, Wednesday, I like Wednesdays');
    checkTime.setValue("06:00:00");  // use this method to convert a string time to a GlideTime

    // now you can accurately compare two GlideTime objects
    if (myTime < checkTime) {
        gs.info("The entered time is earlier than the current time.");
    } 
    else {
        gs.info("The entered time is the same as or greater than the current time.");
    }
} 
else if (dayOfWeek == 4) {
    gs.info("ok, so it's Thursday");
    checkTime.setValue("04:00:00");
    if (myTime > checkTime) {
        gs.info("The entered time is greater than the current time.");
    } 
    else {
        gs.info("The entered time is the same as or less than the current time.");
    }
}

 

You should get results that look something like this:

*** Script: so, Wednesday, I like Wednesdays
*** Script: The entered time is the same as or greater than the current time.

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

sabell2012_0-1698677914746.png


Originally published on: 08-05-2015 02:49 PM

I updated the code and brought the article into alignment with my new formatting standard.

3 Comments