Need help with converting date/time format in Script Include

Uttam Sai
Tera Contributor

Hi Everyone,

 

I am using a Client Script and Script Include to check that the Outage Begin date should be with in the related change Window. This should work for all users in different time zones. The Client Script and Script Include works fine if the user's profile date format is yyyy-MM-dd , and doesn't work for any other date formats. Please let me know how to make the Client Script and Script Include work for all date formats.(Note: if the user's profile date format is not "yyyy-MM-dd" , the alert is triggering even when Outage Begin date is with in the related change Window)

Kindly help me with the issue.

1 ACCEPTED SOLUTION

Community Alums
Not applicable

I modified it to take the client side timezone into account. How does it work?

var Checkbegindate = Class.create();
Checkbegindate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	checkStartofChange: function(){
		
		var begin = new GlideDateTime();
		begin.setDisplayValue(this.getParameter('outageStartDate'));
		begin = begin.getNumericValue() - begin.getTZOffset();

		var changeId = this.getParameter('changeId'); //the changeID
		var changeGR = new GlideRecord('change_request');
		changeGR.get(changeId);
		
		var ChangeStart = new GlideDateTime();
		ChangeStart.setDisplayValueInternal(changeGR.start_date);
		ChangeStart = ChangeStart.getNumericValue();
		
		var ChangeEnd = new GlideDateTime();
		ChangeEnd.setDisplayValueInternal(changeGR.end_date);
		ChangeEnd = ChangeEnd.getNumericValue();
				
		if(begin < ChangeStart || begin > ChangeEnd){
			return false;
		}
		else{
			return true;
		}
	},
	type: 'Checkbegindate'
});

 

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Hi.
Can you try this script include?

 

var Checkbegindate = Class.create();
Checkbegindate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	checkStartofChange: function(){
		var begin = new GlideDateTime();
		begin.setDisplayValue(this.getParameter('outageStartDate'));
		var changeId = this.getParameter('changeId'); //the changeID
		var changeGR = new GlideRecord('change_request');
		changeGR.get(changeId);

		var ChangeStart = new GlideDateTime();
		ChangeStart.setDisplayValueInternal(changeGR.start_date);
		
		var ChangeEnd = new GlideDateTime();
		ChangeEnd.setDisplayValueInternal(changeGR.end_date);
		
		if(begin < ChangeStart || begin > ChangeEnd){
			return false;
		}
		else{
			return true;
		}
	},
	type: 'Checkbegindate'
});

 

 

setDisplayValue(String asDisplayed)

Sets a date and time value using the current user's display format and time zone.

setDisplayValueInternal(String value)

Sets a date and time value using the internal format (yyyy-MM-dd HH:mm:ss) and the current user's time zone.

The references are here.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server/c_APIRef

Best regards,

Hi @Community Alums ,

 

Thank you so much for your response.
It works fine if the user's timezone is GMT. and doesn't work for other time zones as it is considering the Offset between the timezone and GMT . Could you please suggest  , what we can do to get this work for all timezones

 

Thank you 🙂 

Community Alums
Not applicable

I modified it to take the client side timezone into account. How does it work?

var Checkbegindate = Class.create();
Checkbegindate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	checkStartofChange: function(){
		
		var begin = new GlideDateTime();
		begin.setDisplayValue(this.getParameter('outageStartDate'));
		begin = begin.getNumericValue() - begin.getTZOffset();

		var changeId = this.getParameter('changeId'); //the changeID
		var changeGR = new GlideRecord('change_request');
		changeGR.get(changeId);
		
		var ChangeStart = new GlideDateTime();
		ChangeStart.setDisplayValueInternal(changeGR.start_date);
		ChangeStart = ChangeStart.getNumericValue();
		
		var ChangeEnd = new GlideDateTime();
		ChangeEnd.setDisplayValueInternal(changeGR.end_date);
		ChangeEnd = ChangeEnd.getNumericValue();
				
		if(begin < ChangeStart || begin > ChangeEnd){
			return false;
		}
		else{
			return true;
		}
	},
	type: 'Checkbegindate'
});

 

Hi @Community Alums ,

 

Thank you so much!!

 

It is working as expected.