Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Email Script Date Format

GMoon
Kilo Sage

Hi all,

 

I need to print out the format of a date in DD/MM/YYYY format. So far I have this but it doesn't work, can someone point me in the direction of what is missing?

 

Thanks

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here
   
var nd = new GlideDateTime();
nd.addDaysLocalTime(16);
var dateToPrint = nd.getDate();
gs.print(dateToPrint.getByFormat('dd-MMM-YYY'));

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



1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@GMoon 

this should work

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here

    var nd = new GlideDateTime();
    nd.addDaysLocalTime(16);
    var date = nd.getDate();
    var dtObj = new GlideDate();
    dtObj.setValue(date);
    var dateFormatted = dtObj.getByFormat('dd/MM/yyyy');
	template.print(dateFormatted);

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

Background Script:

 var nd = new GlideDateTime();
 nd.addDaysLocalTime(16);
 var date = nd.getDate();
 var dtObj = new GlideDate();
 dtObj.setValue(date);
 var dateFormatted = dtObj.getByFormat('dd/MM/yyyy');
 gs.info(dateFormatted);

Working Output:

 

AnkurBawiskar_0-1761908316789.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@GMoon 

this should work

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here

    var nd = new GlideDateTime();
    nd.addDaysLocalTime(16);
    var date = nd.getDate();
    var dtObj = new GlideDate();
    dtObj.setValue(date);
    var dateFormatted = dtObj.getByFormat('dd/MM/yyyy');
	template.print(dateFormatted);

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

Background Script:

 var nd = new GlideDateTime();
 nd.addDaysLocalTime(16);
 var date = nd.getDate();
 var dtObj = new GlideDate();
 dtObj.setValue(date);
 var dateFormatted = dtObj.getByFormat('dd/MM/yyyy');
 gs.info(dateFormatted);

Working Output:

 

AnkurBawiskar_0-1761908316789.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

GMoon
Kilo Sage

Big thanks Ankur, that's worked perfectly.