Change the date format using server script

RK41
Tera Expert

Hi,

I have one date field variable in my catalog item. The default format of date field is "12-APR-2022" when i try to get this variable using script.

But I want to convert this format like below using script include. Please share your thought on this.

 "2022-04-12"

 

Thanks,

10 REPLIES 10

dmathur09
Kilo Sage
Kilo Sage

Hi RK,

Use the below code

var date2 = date.getByFormat('yyyy/MM/dd');//now use the date2 to set the field value

Regards

Deepankar Mathur

Hi Deepan,

I am trying to pass the variable value "12-APR-2022" from client script to script include.

 

Then I have used below script.

Client script:-  (Roughly I added this code for simple understanding)

ajax.addParam('sysparm_date', "12-APR-2022");
alert(answer); //returned value from script include


Script include:-

var choosenDate = this.getParameter('sysparm_date').toString(); //Get the selected date
		
var gd1 = new GlideDate(choosenDate);
var formattedDate = gd1.getByFormat('yyyy/MM/dd');

return formattedDate.toString();

 

I am getting "NULL" in alert message with Unhandled exception in GlideAjax error.

Hi,

Is your script include client callable?

share complete scripts for both client script and script include

Regards
Ankur

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

Hi Ankur,

Yes my script include is client callable. There is no problem between the client script and script include. I am able to get the return value from script include to client script. Only issue is, I am not able to convert the date as per my expectation.

After formatting this, I just wanted to query the incident table and check how many incident record available in particular date which was selected by user in catalog item form.

u_glide_date_time - This is custom field in incident table.

 

we can't use this format "2022-APR-31" in the query condition of between.

we have to use "2022-04-31" this format.

I don't want to change the system date format, just I want to convert this "2022-APR-31" to "2022-04-31" format in script include.

Expectation:-
var incident = new GlideRecord("incident");
incident.addEncodedQuery("sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^u_glide_date_time_1BETWEENjavascript:gs.dateGenerate('2022-04-31','00:00:00')@javascript:gs.dateGenerate('2022-04-31','23:59:59')");
incident.query();
return incident.getRowCount();

Current behaviour:- 
var incident = new GlideRecord("incident");
incident.addEncodedQuery("sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^u_glide_date_time_1BETWEENjavascript:gs.dateGenerate('2022-APR-31','00:00:00')@javascript:gs.dateGenerate('2022-APR-31','23:59:59')");
incident.query();
return incident.getRowCount();