Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Date/Time not returning expected value to Client Script

Calvert
Tera Contributor

I have what looks to be a simple client script that is passing a date to script include though GlideAjax.

I pass the date into a GlideDateTime and then return the getDisplayValue() to the Client Script.

In the Client script I alert the date, but the date is now different from the one I collected from the form. I did not manipulate the date at all why is it different returning from the script include?

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //    if (oldValue == '') {
    var newDate = g_form.getValue('adjusted_deployment_start');
    alert(newValue);
    alert(newDate);
    var ga = new GlideAjax('ApacClientHelper');
    ga.addParam('sysparm_name', 'calcAdjustedDeploymentEnd');
    ga.addParam('sysparm_date', newDate);
    ga.getXMLAnswer(updateEndDate);
    //    }
}

function updateEndDate(answer) {
    var returnedDate = answer;
    alert(answer);
}

Script Include

	calcAdjustedDeploymentEnd: function() {
		var gdt = new GlideDateTime(this.getParameter('sysparm_date'));
		return gdt.getDisplayValue();
    },

The first two alerts on the client script show the correct time entered into the field

Calvert_1-1699481379987.png

The Third alert after the script include is not the same time anymore

Calvert_2-1699481411850.png

 

Additional Details: The time has been adjusted by -7 hours, and that is our timezone UTC-7. And our system default timezone is UTC-7 as well.

1 ACCEPTED SOLUTION

Hi @Calvert 

So let's try these variables g_user_date_time_format and g_user_date_format.

 

var resolved_date_time = getDateFromFormat(g_form.getValue('resolved_at'), g_user_date_time_format);
var objResolvedDate = new Date(resolved_date_time);
objResolvedDate.setHours(objResolvedDate.getHours() + 3); //+3 hours
var new_resolved_date_time = formatDate(objResolvedDate, g_user_date_time_format);

 

 

You can also convert to system format and pass to the Script Include to calculate.

 

var resolved_date_time = getDateFromFormat(g_form.getValue('resolved_at'), g_user_date_time_format);
var objResolvedDate = new Date(resolved_date_time);
var new_resolved_date_time = formatDate(objResolvedDate, "yyyy-MM-dd hh:mm:ss");

 

Screenshot 2023-11-10 at 00.48.41.png

 

 

Cheers,

Tai Vu

View solution in original post

11 REPLIES 11

I want to add 3 hours to the date/time and return it. But I started with just returning the same date/time to test.

Also it needs to return the correct time regardless of the timezone or date format (dd/mm/yyyy or mm/dd/yyyy) the user is in.

Shubham Singh
Mega Guru

Hi @Calvert 

 

Try this in your Script Include. It will give you the correct results:

calcAdjustedDeploymentEnd: function() {
		var gdt = new GlideDateTime();
		return gdt.setDisplayValue(this.getParameter('sysparm_date'));
    },

 

Thanks!

 

Mark it as helpful if it works 🙂