Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

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.