We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Email Script Date Format

Gem Edwards
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

@Gem Edwards 

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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron

@Gem Edwards 

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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Gem Edwards
Kilo Sage

Big thanks Ankur, that's worked perfectly.