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

Hi @Radhika11,

 

As per my understanding, we have to give date in below string format. It should not contains AM/PM. Then it will be flexible and gives us expected results.

 

var date = "12-31-2023 11:03:19";

 

Note: By default GlideDateTime() will take time as 24 hrs.

 

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

Good morning @Sagar Pagar 

Thank you for continuing providing assistance. User select Date/Time from a Calendar which is Date/Time Type. It will contains AM/PM.  It there a workaround to make it show this works? 

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

Hello @Sagar Pagar 

It is working the way it should be working.

You are a great helper!

We are sincerely appreciate your time and effort. 

Thank you