Auto populate date based on user time zone.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 02:06 AM
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);
}
}
}
- Labels:
-
Service Catalog
-
Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 03:58 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 04:04 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 03:22 AM
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.