Need help on formatting date and time using email script.

Radhika11
Tera Contributor

Hello,

Could some one please help to format date and time in email script?

 

Current Print format:  2023-07-31 07:00 03

Would like to change to this format:   Monday, July 31. 2023 7:00 PM

 

Here is the email script:  

var startDate = current.variables.select_start_date_and_time;
    template.print('<span style="color:#2f5597">' + startDate);

 

Thank you

 

1 ACCEPTED SOLUTION

Hi @Radhika11,

 

For workaround you can try the below scripts,

//  var date = "2023-10-31 07:00:03";
// Monday, July 31. 2023 7:00 PM
//
var date = "2023-07-31 11:03:19 PM";

if (new GlideDateTime(date).isValid()) {
    
	var today = new GlideDateTime(date);
	gs.info(today);

	var day = today.getDayOfMonthUTC();
	var month = today.getMonthLocalTime();
	var year = today.getYearLocalTime();
	var dayofWeek = today.getDayOfWeekLocalTime() % 7;
	var time = today.getTime();
	var timeCreated = time.getByFormat('HH:mm:ss a');

	var Timesplit = timeCreated.split(":");
	var strTime = Timesplit[0] + ":" + Timesplit[1] + ":" + Timesplit[2];

	// Get the name of the month
	var monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	var monthName = monthList[month - 1];

	var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
	var dayName = weekday[dayofWeek];

	// Set the date for the notification

	var requiredDate = dayName + ', ' + monthName + ' ' + day + '. ' + year + ' ' + strTime;
	gs.info(requiredDate);
} else {
	gs.info("not valid date");
}


If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

The world works with ServiceNow

View solution in original post

13 REPLIES 13

Ratnakar7
Mega Sage
Mega Sage

Hi @Radhika11 ,

To format the date and time in the desired format, you can use the GlideDateTime API in ServiceNow. Here's an example of how you can modify your email script to achieve the desired format:

 

var startDate = current.variables.select_start_date_and_time;
var formattedStartDate = new GlideDateTime(startDate).getDayOfWeekName() + ', ' +
    new GlideDateTime(startDate).getMonthName() + ' ' +
    new GlideDateTime(startDate).getDayOfMonth() + ', ' +
    new GlideDateTime(startDate).getYear() + ' ' +
    new GlideDateTime(startDate).getHourOfDayLocalTime() + ':' +
    new GlideDateTime(startDate).getMinute() + ' ' +
    new GlideDateTime(startDate).getDayPart();

template.print('<span style="color:#2f5597">' + formattedStartDate);

 

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

Good morning @Ratnakar7 

Thank you so much for helping.  I used your suggestion code, but I'm getting the following error:

Start: undefined, undefined 13, 2023 undefined:undefined undefined

Radhika11
Tera Contributor

Hello,

Please help on date and time format in email script.  

The input time is "PM"

Radhika11_0-1689297347261.png

The output as 'AM" .  I don't know where I screwed up.

 

Radhika11_2-1689297501141.png

 

 

 

Hi @Radhika11 ,

 

The GlideDateTime API accepts the 24hrs date/time format as input:
try below code:

var gDate = new GlideDateTime('07-13-2023 017:55:48').getDate();
var gTime = new GlideDateTime('07-13-2023 017:55:48').getTime();
var startdate = (gDate.getByFormat(' EEEE dd MMMMM') + gTime.getByFormat( 'hh:mm:ss a'));
gs.print(' Start: '+startdate);

 

Thanks,

Ratnakar