Need to convert Date field to populate only in EST

Mounica2
Tera Contributor

Hi

I need to make a date/time field to populate only in EST and not change according to the user time zone who selects them. I have written a client script and script include to achieve this but I am getting by default, 2022-06-01 and time 7.30 hours ahead of the time selected. Can someone please guide mw with the correct query,
Below is the query that I used,

Catalog Client Script : 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

 
var ga = new GlideAjax('getDateInEST');
ga.addParam('sysparm_name', 'getConvertedDate');
ga.addParam('sysparm_date', g_form.getValue('statement_date')); //Getting the Date field here
ga.getXML(getDataResponse);

function getDataResponse(response) {
var favorites = response.responseXML.getElementsByTagName("favorite");
g_form.setValue('account_number_or_feed_id', favorites[0].getAttribute('statement_date'));
}

}

 

Script Include :

var getDateInEST = Class.create();
getDateInEST.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getConvertedDate: function() {
var mydate = new GlideDateTime(this.getParameter('sysparm_date').toString());

//sets to EST
var tz = Packages.java.util.TimeZone.getTimeZone('US/Eastern');
var gdt = new GlideDateTime();
gdt.setTZ(tz);
var abc = gdt.getDisplayValue();
var favs = this.newItem('favorite');

favs.setAttribute('statement_date',abc);
},
type: 'getDateInEST'
});

 

7 REPLIES 7

suvro
Mega Sage
Mega Sage

It always shows as per the timezone you have set for yourself in your preference. But in the backend it is stored correctly

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

what's your requirement?

The date/time value shown on form is based on user's timezone or the settings from gear icon

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Mounica2
Tera Contributor

Hi @suvro and @Ankur Bawiskar the requirement is that, "Statement Date(date/time)" field should not change based on the viewer, but it should reflect the date the user entered regardless of the viewers time zone(Always in EST)

Hi,

you can convert it like this to EST

update as this

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

	getConvertedDate: function() {
		var mydate = new GlideDateTime(this.getParameter('sysparm_date').toString());
		var sourceTimeZone = 'GMT';
		var targetTimeZone = 'EST';
		var finalValue = global.rmConvertTimeZone(dateTime, sourceTimeZone, targetTimeZone);
		return finalValue;

	},
	type: 'getDateInEST'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var ga = new GlideAjax('getDateInEST');
	ga.addParam('sysparm_name', 'getConvertedDate');
	ga.addParam('sysparm_date', g_form.getValue('statement_date')); //Getting the Date field here
	ga.getXMLAnswer(function(answer){
		g_form.setValue('account_number_or_feed_id', answer);
	});

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader