We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Convert Selected Date time format into custom format.

Vamshi Krishna2
Tera Contributor

Hi,

when I select the start date from the catalog variable I want to convert it into a custom format in a new variable. 

For example, if the start date is 22/01/2023 11:36:52, I want to convert it to 22/JAN/2023 11:36.

But when I use the below code it's not working. or any simple way to do it

 

Client Script:

var st = g_form.getValue('start_date_time');
var ajax = new GlideAjax('Test');
ajax.addParam('sysparm_name','getstartdate');
ajax.addParam('sysparm_startDate', st);
ajax.getXML(getResponse);
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var month_name = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
var month_number = parseInt(answer.substring(3, 5));

var curr_month = month_name[month_number - 1];

var str1 = answer.substring(0, 3);
var str2 = answer.substring(5, 16);
var dt = str1 + curr_month.toString() + str2;
g_form.setValue('planned_start_time', dt);
}

Script Include:

getstartdate : function() {

var gdt = new GlideDateTime(this.getParameter('sysparm_startDate'));
var gtime1 = new GlideTime();
gtime1.setValue("00:00:00");
gdt.subtract(gtime1);
var gtime2 = gdt.getTime();
var tdg = gdt.getDate();
tdg = tdg.getByFormat('dd/MM/yyyy');
var di = gtime2.getByFormat('hh:mm:ss');
return tdg + " " + di;
},

1 ACCEPTED SOLUTION

-O-
Kilo Patron

This is what I would do:

- Script Include:

 

var GlideDateTimeAjax = Class.create();

GlideDateTimeAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	'customFormatDateTime': function GlideDateTimeAjax$customFormatDateTime () {
		var gdt = new GlideDateTime(),
			unixEpochMilliseconds = +this.getValue();

		gdt.setNumericValue(unixEpochMilliseconds);

		var localDate = gdt.getLocalDate().getByFormat('yyyy/MMM/dd'),
			localTime = gdt.getLocalTime().getByFormat('HH:mm');

		return localDate + ' ' + localTime;
	},

	'type': 'GlideDateTimeAjax',

});

 

- Client Script:

 

var unixEpochMilliseconds = getDateFromFormat(g_form.getValue('start_date_time'), g_user_date_time_format);
var gx = new GlideAjax('GlideDateTimeAjax');

gx.addParam('sysparm_name', 'customFormatDateTime');
gx.addParam('sysparm_value', unixEpochMilliseconds);

gx.getXMLAnswer(function (answer) {
	g_form.setValue('planned_start_time', answer);
});

 

View solution in original post

14 REPLIES 14

Hi Saurabh the script is working in the background but when I test it using script include it's returning 00:00 now. 

Script Include:

VamshiKrishna2_0-1674406599917.png

Client Script:

 

VamshiKrishna2_1-1674406673751.png

Output: 

VamshiKrishna2_2-1674406767392.png

 

Thank you

Hi,
Can you please give me the output of below log message

 

SaurabhGupta_0-1674407200404.png

 

 

 


Thanks and Regards,

Saurabh Gupta

Hi Saurabh please check the log info

VamshiKrishna2_0-1674446246457.png

 

-O-
Kilo Patron

This is what I would do:

- Script Include:

 

var GlideDateTimeAjax = Class.create();

GlideDateTimeAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	'customFormatDateTime': function GlideDateTimeAjax$customFormatDateTime () {
		var gdt = new GlideDateTime(),
			unixEpochMilliseconds = +this.getValue();

		gdt.setNumericValue(unixEpochMilliseconds);

		var localDate = gdt.getLocalDate().getByFormat('yyyy/MMM/dd'),
			localTime = gdt.getLocalTime().getByFormat('HH:mm');

		return localDate + ' ' + localTime;
	},

	'type': 'GlideDateTimeAjax',

});

 

- Client Script:

 

var unixEpochMilliseconds = getDateFromFormat(g_form.getValue('start_date_time'), g_user_date_time_format);
var gx = new GlideAjax('GlideDateTimeAjax');

gx.addParam('sysparm_name', 'customFormatDateTime');
gx.addParam('sysparm_value', unixEpochMilliseconds);

gx.getXMLAnswer(function (answer) {
	g_form.setValue('planned_start_time', answer);
});

 

-O-
Kilo Patron

Note that initially the date format was incorrectly 'yyyy-MMM-dd', now changed to 'yyyy/MMM/dd'.