The CreatorCon Call for Content is officially open! Get started here.

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

Tai Vu
Kilo Patron
Kilo Patron

Hi @Calvert 

You're seems messing up between value and display value in the date time field.

What's happening from your script is that.

1. You get a date time from a specific time zone. => date_time_1

2. Then you initiate that time at UTC => GlideDateTime(date_time_1)

3. And convert it again to your current user's time zone. => gdt.getDisplayValue() => now it becomes date_time_2

Sample. (As you can see it becomes 07:00:00 from 06:00:00 (original)

Screenshot 2023-11-09 at 12.08.46.png

 

So in your function from the script include, just simply return gdt or gdt.getValue().

Sample below.

calcAdjustedDeploymentEnd: function() {
    var gdt = new GlideDateTime(this.getParameter('sysparm_date'));
    gdt.addSeconds(3*60*60); //add 3 hours as your requirement
    return gdt.getValue(); //or gdt
},

 

Cheers,

Tai Vu

Calvert
Tera Contributor

getValue() returns in a date format that they system doesn't accept

Calvert_1-1699542439378.png

 

 

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

Calvert
Tera Contributor

Thank you! This worked for me.

Ankur Bawiskar
Tera Patron
Tera Patron

@Calvert 

what's your requirement here?

you are simply sending a date/time value and again returning date/time value without any manipulation?

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