- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2023 07:36 PM
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: Saturday, November 19. 2023 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);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 12:51 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 10:15 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2023 11:20 PM
Hi @Brian186
go through this reference link https://www.servicenow.com/community/developer-forum/formatting-dates-and-times/m-p/1581437
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 12:06 AM
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);
Ref: setDisplayValueLang(String dateTime, String style, String language)
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 07:27 AM
Hi @Tai Vu
I used the exact code you suggested. Somehow, it is giving me an 'undefined' value.
Thank you helping.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 08:03 AM
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.