Issue in calculating time difference in different timezone

kun1
Tera Expert

Hi Team,

My system timezone is in EST and servicenow timezone is in  IST. Below are the script which calculate time difference but its not working properly.

Catalog Client Script:-

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading)
 
    {
 
        return;
 
    }
 
    //start the validation for star date time.
 
 
    if (newValue != '')
 
    {
        var ga = new GlideAjax('BIIB_CPGResizeConfirmDate');
        ga.addParam('sysparm_name', 'chkstartstopDate');
        ga.addParam('sysparm_time', g_form.getValue('planned_start_date_time'));
        ga.getXML(NewParse);
    }
 
    function NewParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'false') {
            alert("Please select a future date");
            g_form.setValue('planned_start_date_time', '');
 
        }
    }
}
 
Script include:-
var BIIB_CPGResizeConfirmDate = Class.create();
BIIB_CPGResizeConfirmDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    chkCurrDate: function() {//Check if given Date time is in past or not.
        var ActualEndDate = this.getParameter('sysparm_time');
        var dif = gs.dateDiff(gs.nowDateTime(), ActualEndDate, true);
        if (dif < 1800) {
            return false;
        } else {
            return true;
        }
    },
    chkstartstopDate: function() {//Check if given Date time is in past or not for start stop.
        var ActualEndDate = this.getParameter('sysparm_time');
        var dif = gs.dateDiff(gs.nowDateTime(), ActualEndDate, true);
        if (dif < -2) {
            return false;
        } else {
            return true;
        }
    },
    
});
 
it will not calculating time diff properly when system(EST) and servicenow (IST) timezone is diff
22 REPLIES 22

@kun

So what did not work fine?

what debugging did you perform?

Regards
Ankur

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

First issue is on form variable take in time as per my system time but time zone is as per my Servicenow.

 

No worries on that.

UI Policy condition should take that into account.

Regards
Ankur

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

Hi Ankur,

We have tried with the UI policy also. So, if my System time is in EST and servicenow is in IST then the time taken on portal is the browser time(System time) . So, suppose if i select 14/09/2022 2:51 AM then time mention is in IST but time zone is EST . so when EST user open RITM in ITIL view time change from 14/09/2022 2:51 AM to EST time and that is the past time.

IST time is now 14/sep/2022 12:22 PM but in the variable time taken from my system time(est) which is past time

 

find_real_file.png

 

it should show error here that please select future date.

 

After submission:-

User having IST time zone

find_real_file.png

 

User having EST timezone

find_real_file.png

 

i think you understand the issue.

AnubhavRitolia
Mega Sage
Mega Sage

Hi Kun,

You can try below script to convert your field time zone to required time zone:

var gDate = Packages.com.glide.glideobject.GlideDateTime(planned_start_date_time);

//converting GMT to Canada/Eastern

var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");

// Put Time Zone based on your requirement

var pDate = gDate.setTZ(tz);

gs.log(pDate); // Just to test the value of Date/Time Field in different Timezone

Than you can compare both fields in same time zone.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023