Catalog client script - How to set the time of a Date/Time variable

KoenHaemels
Tera Contributor

Hi

I'm trying to set the time of a date/time variable in a catalog client script.
However, the date gets changed and set. But the time remains on 00:00:00.

var dateNumber = getDateFromFormat(g_form.getValue('temp1'), g_user_date_format);
var date = new Date(dateNumber);

// Initialize a new Date object
var newDateTime = new Date(date.getFullYear(), date.getMonth(), date.getDate());

// Set the time to 23:59:59
newDateTime.setHours(23);
newDateTime.setMinutes(59);
newDateTime.setSeconds(59);

// Set the value in the datetimeField
g_form.setDisplayValue('temp2', newDateTime);


Temp1 is a date variable and temp2 is a date/time variable.
The script gets the date that is selected in temp1 and puts it in temp2 and should add the time 23:59:59.
However the date is set but the time is not.

Regards

15 REPLIES 15

KoenHaemels
Tera Contributor

@Saloni Suthar & @Community Alums changing Date to GlideDateTime is giving me next error: ReferenceError: GlideDateTime is not defined

So this doesn't work?

As said by me @KoenHaemels , it is a server side script and you are using directly in Client script so it won't work

rather you have to use via GlideAjax.

 

You can use this article if this won't work will help you with the code.

https://www.servicenow.com/community/developer-forum/glideajax-to-add-date-time-is-adding-to-today-s...

 

If my response help you please mark it correct and hit the helpful button

 

Thanks and regards,

Saurabh Dubey.

Hi @saurabh_dubey can you help to convert the code?
I have zero experience with GlideAjax

Okay..... stay tuned work in progress

Hi @KoenHaemels , 

 

here is the code;

 

Onchange Client script: 

 

Name: Date time saga

Table : incident (your own choice)

UI Type : All

Type : onChange

FieldName : u_temp1 // your field

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

	var currentDate = g_form.getValue('u_temp1');

	var ga = new GlideAjax('incidentUtil');
    ga.addParam('sysparm_name', 'addDate');
    ga.addParam('sysparm_temp1', currentDate);

	ga.getXMLAnswer(getResponse);

	function getResponse(response){
		alert(response);

		if(response){
			g_form.setValue('u_temp2',response);
		}
	}

    //Type appropriate comment here, and begin script below

}

 

Script includes : 

 

Name : incidentUtil

Client callable : tick

Once you save it will as for the role : Type your role there for whom you want this script to work

var incidentUtil = Class.create();
incidentUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	addDate : function(){
		var currentDate = this.getParameter("sysparm_temp1");

		var gTime = new GlideDateTime(currentDate + " 23:59:59");

		return gTime.getValue();

	},

    type: 'incidentUtil'
});

 

Here this will work... 

 

result.

saurabh_dubey_0-1708328228725.png

 

Don't forget to mark my answer helpful, if the solutions is fine do accept my solution

 

Thanks

Saurabh Dubey