How to subtract one day from a date value - scoped application email script

Nestor Padilla1
Giga Expert

Hello ServiceNow Experts,

I've been struggling to subtract one day from a date value I get from a custom table in a Scoped Application.

The idea behind this is that if the "end date" value I get falls on a Sunday, then in the email notification I should add some text to do something on Saturday.

For that I need to subtract one day from the end day if end day is a Sunday.

With this code I can validate if it is Sunday 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var endDate = new GlideDate();
	endDate.setValue(current.planned_end);
	var dateString = endDate.getByFormat('EEEE, MMM d'); // e.g. Friday June 3
	var sunday = dateString.indexOf('Sunday');
	if (sunday > -1) {
		var saturday = endDate.addDaysLocalTime(-1);
		
	}

    if (sunday > -1) {
        template.print("<br /><p><strong><span>Since this observance includes Sunday, Operators have the option of:</strong></span></p><ul><li>Removing the American flag at sunset on " + saturday.getByFormat('EEEE, MMM d') + "</li><li>Returning the flag to full-staff at sunset on " + dateString + "</li>");
    } else {
        return;
    }

})(current, template, email, email_action, event);

I'm sure I have something wrong in my code. I was hoping someone could help me fix it.

I can't get to subtract 1 day from the end date and then format that result. Maybe my understanding of how date/time data is handled is wrong.

Thanks 

1 ACCEPTED SOLUTION

Nestor Padilla1
Giga Expert
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
		/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
		/* Optional GlideRecord */ event) {
	
	var todayDate = new GlideDate().getByFormat('EEEE, MMM d');
	var endDate = new GlideDate();
	endDate.setValue(current.planned_end);
	var dateString = endDate.getByFormat('EEEE, MMM d'); // e.g. Friday June 3
	var sundayString = dateString.indexOf("Sunday");

	var getSaturday = new GlideDateTime(current.planned_end);
	getSaturday.addDaysLocalTime(-0);
	var saturday = getSaturday.getLocalDate();
	
	if (sundayString > -1) {
		
		template.print("Remove it on " + saturday.getByFormat('EEEE, MMM d') + ", and BLAH BLAH BLAH); // The output is Remove it on Saturday, Month Day ie Saturday Jun 19

		} else {
			return;
		}

})(current, template, email, email_action, event);

I ended up with the following code and works OK.

For some reason when I set getSaturday.addDaysLocalTime(-1) I get Friday. So I guess SN counts starting from 0.

I hope this helps anyone with the same issue.

View solution in original post

5 REPLIES 5

OlaN
Giga Sage
Giga Sage

Hi,

The class GlideDate does not support methods to add/remove days as you are trying to do.

Instead switch to use a GlideDateTime object, which can be used to add days like you have written in your code.

Hi OlaN,

Thanks. Your statement put me in the right direction.

Here is what I ended up with

	var endDate = new GlideDate();
	endDate.setValue(current.planned_end);
	var dateString = endDate.getByFormat('EEEE, MMM d'); // e.g. Friday June 3
	var sundayString = dateString.indexOf("Sunday");

	var getSaturday = new GlideDateTime(current.planned_end);
	getSaturday.addDaysLocalTime(-0);
	var saturday = getSaturday.getLocalDate();
	
	
	
	if (sundayString > -1) {..... do something... 

I have been testing it and it works fine.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Nestor,

Try the following script.

ServiceNow supports timezones so it's necessary to use GlideDateTime() because day may change based on which timezone of the end user.

When converting GlideDate() to GlideDateTime(), it is necessary to explicitly specify time or ServiceNow will use the current time which may cause wrong result. That is, if the current date is June 5, 2022 and time of the execution is 23:59, the datetime will become 2022-06-05 23:59:00 and if the timezone is not UTC, the date may become 2022-06-06. 

I've also used getDayOfWeekLocalTime() to get days of week.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var endDate = new GlideDate();
    endDate.setValue(current.planned_date);
    var endDateTime = new GlideDateTime(endDate.getValue() + ' 00:00:00');  // need to explicity add time or current time will be used

    if (endDateTime.getDayOfWeekLocalTime() == 7) {  // use getDayOfWeekLocalTime() to get day of week. 
        var saturday = endDateTime.addDaysLocalTime(-1);
        template.print("<br /><p><strong><span>Since this observance includes Sunday, Operators have the option of:</strong></span></p><ul><li>Removing the American flag at sunset on " + saturday.getByFormat('EEEE, MMM d') + "</li><li>Returning the flag to full-staff at sunset on " + dateString + "</li>");
    } else {
        return;
    }
})(current, template, email, email_action, event);

Hi Hitoshi,

WOW, Thanks a lot for that detailed explanation. In fact, I did some additional tests to my code, and what you brought up is exactly what happens

Here is my code:

	var endDate = new GlideDate();
	endDate.setValue(current.planned_end);
	var dateString = endDate.getByFormat('EEEE, MMM d'); // e.g. Friday June 3
	var sundayString = dateString.indexOf("Sunday");

	var getSaturday = new GlideDateTime(current.planned_end);
	getSaturday.addDaysLocalTime(-0);
	var saturday = getSaturday.getLocalDate();
	
	
	
	if (sundayString > -1) {

I will test with yours and let you know the results.

Thanks again. I really struggle when it comes to DateTimes in ServiceNow