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

Periyasamy P
Tera Guru

You can try to below script.

var tz = gs.getSession().getTimeZone();

var gdt = new GlideDateTime();

gdt.setTZ(tz);

gdt.add(60*60*24*N*1000); //N- Number of days, Input in milliseconds

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can do this without script include

Your onChange is running on which variable?

how will you determine 1 days or 7 day

Regards
Ankur

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

Thank you for the reply.

I am using Date variable.

there is a select box to determine the day-1 and day-1 depending upon dropdown selection

 

Regards,

Siddhesh

@Siddhesh Gawade 

so onChange is written on which variable?

what's the other variable name for select box? what are the choice values for Day 1 and Day 7?

Regards
Ankur

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