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

Hi @Peter Bodelier 

I have also tried your code; however, a 'blank' value was returned

We will do the update soon to make things easier.

Thank you

 

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 

Hi @Tai Vu ,

Your suggestion is correct.  Thank you

Brian186_0-1700504997935.png

 

Hi @Amit Gujarathi ,

I have tried your suggestion code and we are getting "undefined" value.  

Brian186_0-1700503029840.png

We will do the update soon to make things easier.

 

Thank you