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

AnubhavRitolia
Mega Sage
Mega Sage
Can you please share the screenshot of both date fields with values in the format it is set.
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

find_real_file.png

 

My laptop timezone is in EST and Servicenow timezone is in IST and it took my system time in variable.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

why not use UI policy and no scripting will be required?

Something like this; it's just an example. Please enhance it

Start Time [After] Today

find_real_file.png

 

function onCondition() {

	// valid
	alert('Start date cannot be in future');
	g_form.showErrorBox('start_date_time','Invalid');
	g_form.setMandatory('start_date_time', true);

}

find_real_file.png

Regards
Ankur

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

tried with this way also but no luck