GMT TimeZone of Date/Time field being returned from Ajax Call, called by Client Script

Kevin133
Tera Expert

I've spent hours and hours looking at post after post and developer site after developer site, and Glidesystem calls galore...

I have a client script in my catalog that will take the due_date value, add x number of days, then return that date/time value in to a variable in my form via an AJAX call to a script include.

Obviously, the due_date is an OOTB field, and displays nicely in my Timezone.

The issue: the server is returning the date/time in GMT. 

I've tried all the usual suspects

gs.getSession().getTimeZone()
setTZ()
addDaysLocalTime()
getDisplayValue()

 

KEEP IN MIND: this is a DATE and TIME field. Not just date, and not just time.

 

My Script Include (called RemovalDate)

    addDays: function() {
 
        var eDate = this.getParameter('sysparm_start');
        var addDays = this.getParameter('sysparm_days');
        var gdt = new GlideDateTime(eDate);
        gdt.addDaysLocalTime(addDays);

        return gdt.getDisplayValue();
    },

 

My Client Script

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {
        return;
    }
    var cdt = g_form.getValue('due_date');
    alert(cdt);
    var ga = new GlideAjax('RemovalDate');
    ga.addParam('sysparm_name', 'addDays');
    ga.addParam('sysparm_start', cdt);
    ga.addParam('sysparm_days', '1');
    ga.getXML(parseResponse);
}

function parseResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert(answer);
    g_form.setValue('past_date', answer);
}

 

Here is the due date on the record

find_real_file.png

 

Here is the value it's returning to validate yes, it's sending that due date to the script

find_real_file.png

 

here is what is returning from the server when it adds 1 day

find_real_file.png

It's populating that exact date/time in the variable

find_real_file.png

That's true, if i were in GMT. I'm in MST and that's what my profile is set to, as per the due_date

Thoughts?

 

1 ACCEPTED SOLUTION

Kevin133
Tera Expert

I Pray to everything holy that anyone having this issue sees this post. After countless hours of work, i finally got it working.

 

So here is my client script (obviously mine is on change of a variable. Your's can be whatever)

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {
        return;
    }
    var cdt = g_form.getValue('due_date');
	alert(cdt);
    var ga = new GlideAjax('RemovalDate');
    ga.addParam('sysparm_name', 'addDays');
    ga.addParam('sysparm_start', cdt);
    ga.addParam('sysparm_days', '-5'); // I actually want to subtract days from an existing field.
    ga.getXML(parseResponse);
}

function parseResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert(answer);
    g_form.setValue('past_date', answer);
}

 

Here is my Script Include

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

    addDays: function() {
        var eDate = this.getParameter('sysparm_start');
        var dta = this.getParameter('sysparm_days');

        // Get Time Offset
        var gdtLocal = new GlideDateTime();
        gdtLocal.getLocalTime(); // get the users local time (per their time zone setting)
        var offset = gdtLocal.getTZOffset(); //this, for example would = 6 for an MST User

        //get Date/Time
        var gdt = new GlideDateTime(eDate);
        gdt.addDays(dta); // add (or subtract if a - number) from eDate
        gdt.add(-+offset);//subtract the offset
        return gdt.getDisplayValue(); //return the date in the user format
    },
    type: 'RemovalDate'
});

View solution in original post

10 REPLIES 10

Hello Aara,

I'm a little confused by your question and what you show as your script include.

You say you want to get date only back, but your requirement is to get users time zone, but looks like your just trying to determine if the selected date is in the future so don't really need to return a date, but a true/false of sorts.

Also, your naming conventions are a little confusing because you are calling both your SI, Function AND variable the same name.

 

First, I wouldn't create a Script Include function with the name Date. It's ambiguous. make it something that you can re-use. Same for the Script Include. Make it something that you can add many functions in so you have one place for all your functions (I do ChangeUtils, IncidentUtils, RequestUtils, etc...) this way, you dont have to create a SI for every single function.

 

Try This 

Create a SCRIPT INCLUDE

Name: RequestUtils, client callable=true

Place this function in it

 

checkDate : function() {
	var start = this.getParameter('sysparm_date');
	var currDay = gs.now();
	var dateDiff = gs.dateDiff(currDay, start, true);
	if(dateDiff < 0) {
		return false;
	}
	else if(start != currDay)
	{ 
		return true; 
	} 		
},

 

CLIENT SCRIPT

Set your client script to be an on change of the hold_until variable.

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

   if(newValue != '') {
		var ga = new GlideAjax('RequestUtils');
		ga.addParam('sysparm_name', 'checkDate');
		ga.addParam('sysparm_date', newValue);
		ga.getXML(ajaxResponse);
	}
}

function ajaxResponse(response){
	var answer = response.responseXML.documentElement.getAttribute("answer");
	if(answer == 'false'){
		g_form.showFieldMsg('hold_until','Date in the past is not allowed.','error',true);
                g_form.clearValue('hold_until');
	}
}

 

aarya1
Giga Contributor

Hi Kevin

So by this code we can select only future date right?

Past date wont select and current date also wont select .

Correct

aarya1
Giga Contributor

Hi Kevin

As i used above code but past date and current date also selecting.

Instead of using

	g_form.showFieldMsg

Try to use Alert. This way you can see what is actually returning.

alert("You must select a date in the future." + answer);

If you arn't getting a "true" or "false" back, then something wrong with your SI

If you arn't getting any popup at all, then check to make sure you are "onchange" and "variable name" = the field you are wanting to check

If you ARE getting an answer back, then

change this

if(dateDiff < 0) {

to this

if(dateDiff <= 0) {

adding The = will also make the current day = false.

 

Your script include should look like this provided you created a new one and called it RequestUtils

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

	checkDate : function() {
		var start = this.getParameter('sysparm_date');
		var currDay = gs.now();
		var dateDiff = gs.dateDiff(currDay, start, true);
		if(dateDiff <= 0) {
			return false;
		}
		else if(start > currDay)
		{ 
			return true; 
		} 		
	},

	type: 'RequestUtils'
});

Your client script should look like this MAKE SURE IT IS "ONCHANGE" on hold_until variable (If that's your variable) if not, make sure you change the variable name in the script

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

   if(newValue != '') {
		var ga = new GlideAjax('RequestUtils');
		ga.addParam('sysparm_name', 'checkDate');
		ga.addParam('sysparm_date', newValue);
		ga.getXML(ajaxResponse);
	}
}

function ajaxResponse(response){
	var answer = response.responseXML.documentElement.getAttribute("answer");
	if(answer == 'false'){
		alert("You must select a date in the future.");
                g_form.clearValue('hold_until');
	}
}