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

Brad Bowman
Kilo Patron
Kilo Patron

I ran into the same issue with a very similar-looking script include.  Against all logic I finally tried changing the return value out of curiosity, and it worked for me.

return gdt;

I had tried that, but then it returns in system format: yyyy-mm-dd hh:mm:ss

find_real_file.png

and when i try to save the record, it i get message: 

find_real_file.png

 

It's expecting mm-dd-yyy hh:mm:ss

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'
});

aarya1
Giga Contributor

Hi kevin 

How can we get only date through scriptinclude and by using glideajax in clientscript.

My requirement is to get users time zone 

Actualy iam using below script include and iam not getting the users time zone .

Script include :

date: function() {
var dt = this.getParameter('date');

var gdt1 = new GlideDate(date);
var gdt2 = new GlideDateTime().getDate();
var gdt3 = gdt2.getDisplayValue();
if (gdt1 >= gdt3) {

var answer = 1;
} else {
answer = 0;
}
return answer;

 

Client script:

var validTo = g_form.getValue('hold_until');

var gr = new GlideAjax('global.HoldUtil');
gr.addParam('sysparm_name', 'date');
gr.addParam('date', validTo);
gr.getXML(GetDate);

function GetDate(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer == 0) {
g_form.clearValue('hold_until');
g_form.showFieldMsg('hold_until', 'Date is invalid. Please select a future date.', 'error', true);

 

Please suggest with your answer

Regards 

Aarya