Outage time format in email notification

Luiz Lucena
Mega Sage

Hello everyone, 

 

We have a requirement to update the email notification to show only time the outage begin or end.

For this, I created a mail script to parse only the hour:minutes from the Outage Begin field.

This part is working fine, but is passing in 24h format, like below:

LuizLucena_0-1715373239615.png

Here is the mail script I did:

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

var curGdt = current.end.getDisplayValue().substring(11, 16);
template.print(curGdt);

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

How should I do to pass the time in 12h format including AM/PM?

9 REPLIES 9

Community Alums
Not applicable

Hi Luiz,

 

This should achieve your desired outcome. Since there is no getByFormat() method available for GlideDateTime, we can pass the curGdt GlideDateTime object to the GlideDate method and return the time formatted with AM/PM.

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

    var curGdt = new GlideDateTime(current.end);
    var userFormattedDate = formatDate(curGdt);

    template.print("Outage end: " + userFormattedDate);
    
}
)(current, template, email, email_action, event);

function formatDate(date) {

var gdt = new GlideDate();
gdt.setValue(date);
var time = gdt.getByFormat('hh:mm a'); 

return time;
}


Regards,
Kyle

Hi @Community Alums 

Thanks, very helpful, we got very close, but is returning the UTC time instead of the system time, US Eastern time. Do you happen to know why?

Community Alums
Not applicable

Hi @Luiz Lucena ,

You can refer below code to change or set your current timeZone 

var tz = gs.getSession().getTimeZone();
var gdt = new GlideDateTime(); 
gdt.setTZ(tz);

 

Please mark my answer correct and helpful if this works for you

Thanks and Regads 

Sarthak

Thanks, @Community Alums 

Will try to include this piece into the one you provided earlier.

Here is the version I tried, but it insists in showing UTC time.

 

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

    // Add your code here
	var tz = gs.getSession().getTimeZone();
    var curGdt = new GlideDateTime(current.begin);
    var getDate = new GlideDate();
    getDate.setValue(curGdt);
	getDate.setTZ(tz);
    var getTime = getDate.getByFormat('hh:mm a');

    template.print(getTime);

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