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

Hello @Peter Bodelier 

You are correct; we are still on the Tokyo release. Is there a workaround for the Tokyo release?

Thank you

Hi @Brian186 

 

You could use plain javascript, to gather the info and display it in the format you need, however. Since Tokyo is end of life (I believe as of today) you should simply upgrade 😉


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

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



@Amit Gujarathi Nice! 👍 getByFormat works with GlideDate isn't it?

Hi @Tai Vu 

 

Yes, that is only GlideDate.
You could try this, and adapt it to your use case, however I still suggest upgrading and using a OOTB oneliner...

 

function formatDateTime(dateTimeString) {
  var gdt = new GlideDateTime(); 
  gdt.setValue (dateTimeString);
   var ms = gdt.getNumericValue();

  // Create a JavaScript date object from the given datetime string
  var dateTime = new Date(ms);

  // Get the day of the week
  var dayOfWeek = dateTime.getDay();
  var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  var formattedDayOfWeek = days[dayOfWeek];

  // Get the month
  var month = dateTime.getMonth();
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var formattedMonth = months[month];

  // Get the year
  var year = dateTime.getFullYear();

  // Get the hours
  var hours = dateTime.getHours();

  // Convert hours to 12-hour format
  var formattedHours = hours;
  if (hours > 12) {
    formattedHours = hours - 12;
  } else if (hours === 0) {
    formattedHours = 12;
  }

  // Get the minutes
  var minutes = dateTime.getMinutes();
  var formattedMinutes = minutes < 10 ? ("0" + minutes) : minutes;

  // Get the AM/PM indicator
  var amPm = hours < 12 ? "AM" : "PM";

  // Format the date and time
  var formattedDateTime = formattedDayOfWeek + ", " + formattedMonth + " " + dayOfWeek + ", " + year + " at " + formattedHours + ":" + formattedMinutes + " " + amPm;

  return formattedDateTime;
}

// Example usage
var dateTimeString = "2023-11-19 03:03:15";
var formattedDateTime = formatDateTime(dateTimeString);
gs.print(formattedDateTime);

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