Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Convert Date format in Script Include

Uttam Sai
Tera Expert

Hi All,

 

I have been using the below Script Include to compare begin of Outage with Scheduled change start time.
This works if the Date format is yyyy-MM-dd , which is default. In Some cases , the users personalize the date format to other  and the script include doesn't work as expected in these cases.Could you please help me how to convert the Date-Time format to yyyy-MM-dd from other in the Script Include before comparing.

Thank you 🙂

 

Script Include:

var OutageAjaxUtils = Class.create();
OutageAjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkStartofChange: function(){
var begin = this.getParameter('outageStartDate'); //the begin date of outage sent from the client script
var changeId = this.getParameter('changeId'); //the changeID

var changeGR = new GlideRecord('change_request');
changeGR.get(changeId);

//check if the begin date is less than the change start date
if(begin < changeGR.start_date){
return false;
}
else{
return true;
}
},

type: 'OutageAjaxUtils'
});

2 REPLIES 2

reshmapatil
Tera Guru

Hi @Uttam Sai ,

 

You can use the below piece of code to format the date to yyyy-MM-dd.

var gd = new GlideDate();

gd.setValue('2021-04-21');

gs.info(gd.getByFormat("yyyy-MM-dd"));

 

For more check:

https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_GlideDateScopedAP...

 

 

Regards,

Reshma

**Please mark my answer correct or helpful based on the impact**

-O-
Kilo Patron

One should do the correct interpretation of date (or date/time) inputs on client side. In client script a function and several global variables are available that help with this. The function is called getDateFromFormat and the global variables are g_user_date_format and g_user_date_time_format. The function returns a number, a Unix epoch date/time. One can use that to initialize a GlideDateTime server side - and to get the same date, of course.

So on client side something like:

 

 

var milliseconds = getDateFromFormat(g_form.getValue('<date field name>'), g_user_date_format);
var gx = new GlideAjax('OutageAjaxUtils');

gx.addParam('sysparm_name', 'checkStartofChange');
gx.addParam('sysparm_start_date', milliseconds);

gx.getXMLAnswer(...);

 

 

On server side:

 

 

var OutageAjaxUtils = Class.create();

OutageAjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkStartofChange: function () {
		var begin = +this.getParameter('sysparm_start_date'); //the begin date of outage sent from the client script
		var beginDate = new GlideDateTime();
		var changeId = this.getParameter('changeId'); //the changeID

		beginDate.setNumericValue(begin);

		var changeGR = new GlideRecord('change_request');
		changeGR.get(changeId);

		//check if the begin date is less than the change start date
		if (begin < changeGR.start_date) {
			return false;
		}
		else {
			return true;
		}
	},

	type: 'OutageAjaxUtils'

});

 

 

Also it is best to use dedicated methods to compare dates as opposed to comparing those as strings, like:

 

GlideDateTime.subtract()

 

In that case you can directly use the date numeric value too.