Auto populate date based on user time zone.

Siddhesh Gawade
Mega Sage
Mega Sage

Hello 

I am using a field "end date" on a catalog item form, which shows date dependant on selection(day1 or day7). if the selection in 'day1' then end date field shows (current date + 1day) adding one day in current date. or if 'day7' selected end date field shows (current date +7days) adding seven days in current date.

I used a script include and call it in catalog client script but it takes dates as per "GMT" time zone but I want date should be as per users time zone .So if the  date is as per "GMT" time zone if the catalog form is submitted within specific time period it adds 2-days or 8-days instead of 1 0r 7days.

script include to add one day :

var adding_a_day_to_the_current_date = Class.create();
adding_a_day_to_the_current_date.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calDate: function() {
var sdt = new GlideDate();
sdt.addDaysLocalTime(1);
var gdt=sdt.getDisplayValue();
return gdt;

},
type: 'adding_a_day_to_the_current_date'
});

 

catalog client script to add one day :

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

var getDate = new GlideAjax('adding_a_day_to_the_current_date');
getDate.addParam('sysparm_name', 'calDate');
getDate.getXML(callback);

function callback(response) {
var exceptionReq = g_form.getValue('temporary_ongoing_request');
if (exceptionReq == 1) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('end_date', answer);
}
}
}

7 REPLIES 7

onChange is written on "temporary_ongoing_request" variable

 

variable name for select box is : temporary_ongoing_request

variable name for Date is : end_date

choice value for day1 = 1

choice value for day7 = 2

 

regards

Siddhesh

Hi,

here is the script

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

	if(oldValue != newValue){
		var todayDate = new Date(); // Now
		var daysToAdd = parseInt(newValue);
		if(daysToAdd == 2){
			daysToAdd = 7;
		}
		todayDate.setDate(todayDate.getDate() + daysToAdd);
		g_form.setValue('end_date', todayDate.toISOString().slice(0,10));
	}
}

OR

give the choice value for Day 7 as -> 7 instead of 2

then use this

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

    if(oldValue != newValue){
        var todayDate = new Date(); // Now
        var daysToAdd = parseInt(newValue);
        todayDate.setDate(todayDate.getDate() + daysToAdd);
        g_form.setValue('end_date', todayDate.toISOString().slice(0,10));
    }
}

Regards
Ankur

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

Service_RNow
Mega Sage

HI,

try this code:-

1.first get a user time zone

 var myUserObject = gs.getUser();

 myUserObject.getTZ(); // this will give you logged in usr time zone , 

Hope this helps.
please mark my answer as Correct & Helpful based on the validations.