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

Good morning @Ratnakar7 

Thank you for helping.  The Date and Time values are coming from a dropdown and it is formatted as 12 Hours.  

 

Radhika11_0-1689610952732.png

 

How to use script to convert 23 HRS into 12 HRS Date/Time format as an input so that the output come out correctly?

 

Sagar Pagar
Tera Patron

Hi @Radhika11,

 

I have tried in background scripts and working as expected.

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

var today = new GlideDateTime(date);
var day = today.getDayOfMonthUTC();
var month = today.getMonthLocalTime();
var year = today.getYearLocalTime();
var dayofWeek = today.getDayOfWeekLocalTime();
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;
template.print("date: " + requiredDate);

 

Output:

*** Script: Monday, July 31. 2023 07:00:03 AM

 

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

Hello @Sagar Pagar 

We are very much appreciate your assistance.  We followed you suggestion and used the code provided.  However, we are encountering the following error in background scripts and we don't how to fix it:  Please help.  Thank you

 

Radhika11_0-1689614556653.png

 

Hi @Radhika11,

I have provided script that will be used in Email scripts.

Note: template.print() will work in Email scripts not in background scripts.

 

For background scripts try gs.info();

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

var today = new GlideDateTime(date);
var day = today.getDayOfMonthUTC();
var month = today.getMonthLocalTime();
var year = today.getYearLocalTime();
var dayofWeek = today.getDayOfWeekLocalTime();
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);

 

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

Hi @Sagar Pagar 

Thank you for explaining how the code works. I learned something new today.

I have tested and the majority dates and times are working as expected.   However, when passed in var date = "12-31-2023 11:03:19 PM", I'm getting undefined for date of the week.  Just wanted to know the reason behind it and this is fixable?


*** Script: undefined, December 31. 2023 11:03:19 AM