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

Brad Bowman
Kilo Patron
Kilo Patron

Just add the timezone offset before returning the value:

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

 

Thank you! I'm closer now. The offset moved my date 7 more hours in the wrong direction but adding a * -1 to the offset has the date lined up correctly at least for USA dates.

 

Now I have a problem with dates from Australia, they use dd/mm/yyyy instead of mm/dd/yyyy. But if the date could be switched and still be valid it is switching it to the US order. So I enter November 8th as 08/11/2023 and it returns 11/08/2023 which is August 11th.

 

If the date could not be valid, say November 23rd as 23/11/2023, it keeps the date correct but sets the time to midnight.

Tony Chatfield1
Kilo Patron

Hi, I think Brad has already help resolve your issue.
My question is, why pass a date\time string to a server script with the expectation that it will return a string that is exactly the same as the string passed in?
What is you use case\business requirement?

Yes there is not much to the script as written but this is just the first step.

The use case is when a date field is populated, it populate a second field with the same date and time plus 3 hours. Since I wasn't getting the right date to begin with adding 3 hours to that date wasn't going to be correct, either.