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.

How to format date and time in email script?

Brian186
Tera Expert

Hello,

I have an email script that currently print out date and time as "Start Date: 2023-11-19 03:03:15"

Could someone please help to take a look at the blow code to see if it can be print out in this format?  Thank you

"Start Date: SaturdayNovember 192023 at 03:03 AM"

 

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('sys_id', current.document_id.toString());
ritm.query();
if (ritm.next()) {
template.print("Start Date: " + ritm.variables.select_start_date_and_time);
}

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

2 ACCEPTED SOLUTIONS

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Brian186 ,
I trust you are doing great.
Please find the updated script for the same.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('sys_id', current.document_id.toString());
    ritm.query();
    if (ritm.next()) {
        var gdt = new GlideDateTime(ritm.variables.select_start_date_and_time);
        var formattedDate = gdt.getDisplayValueInternal(); // Gets the date in system format
        // Now format the date as per your requirement
        var userFormattedDate = formatDateToUserReadable(formattedDate);
        template.print("Start Date: " + userFormattedDate);
    }
})(current, template, email, email_action, event);

function formatDateToUserReadable(dateStr) {
    var gdt = new GlideDateTime(dateStr);
    var day = gdt.getByFormat('EEEE'); // Day in full text
    var month = gdt.getByFormat('MMMM'); // Month in full text
    var date = gdt.getByFormat('dd'); // Date
    var year = gdt.getByFormat('yyyy'); // Year
    var time = gdt.getByFormat('hh:mm a'); // Time in 12-hour format with AM/PM

    return day + ", " + month + " " + date + ", " + year + " at " + time;
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

Hi @Brian186 

From Amit's comment, you can replace the GlideDateTime by GlideDate, it should do the trick.

Sample

function formatDateToUserReadable(dateStr) {
    //var gdt = new GlideDateTime(dateStr);
    var gdt = new GlideDate();
    gdt.setValue(dateStr);
    var day = gdt.getByFormat('EEEE'); // Day in full text
    var month = gdt.getByFormat('MMMM'); // Month in full text
    var date = gdt.getByFormat('dd'); // Date
    var year = gdt.getByFormat('yyyy'); // Year
    var time = gdt.getByFormat('hh:mm a'); // Time in 12-hour format with AM/PM

    return day + ", " + month + " " + date + ", " + year + " at " + time;
}

 

Cheers,

Tai Vu 

View solution in original post

13 REPLIES 13

Tai Vu
Kilo Patron
Kilo Patron

Hi @Brian186 

Let's use the API setDisplayValueLang, in order to set a date and time using a specified style, language, and format according to the current user's locale.

Try my adjustment below.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('sys_id', current.document_id.toString());
    ritm.query();
    if (ritm.next()) {
        var gd = new GlideDate();
        gd.setValue(ritm.variables.select_start_date_and_time.getDisplayValue());
        var start_date_time = gd.getByFormat('MM/dd/yyyy hh:mm:ss'); //en-US English United States

        var glideDateUS = new GlideDateTime();
        glideDateUS.setDisplayValueLang(start_date_time, "short", "en-US");

        template.print("Start Date: " + glideDateUS.getDisplayValueLang("full"));
    }

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

 

Screenshot 2023-11-17 at 15.05.42.png

 

 

Ref: setDisplayValueLang(String dateTime, String style, String language)

 

Cheers,

Tai Vu

Hi @Tai Vu 

I used the exact code you suggested. Somehow, it is giving me an 'undefined' value.

Brian186_0-1700234615026.png

Thank you helping.

 

 

Hi @Brian186 

 

setDisplayValueLang ( GlideDateTime | ServiceNow Developers )

Will work starting on Utah.

 

If you're getting undefined, it looks like you're still on Tokyo?


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.