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

Hi,

Can you remove the toString() from the line and try

var choosenDate = this.getParameter('sysparm_date').toString();

Regards,

Deepankar Mathur

Hi,

Its not worked. Forgot about client script and script include.

 

I just created one simple fix script. Can you pls try this in your PDI.

var date = '2022-APR-31';

var gd1 = new GlideDate(date);
var formattedDate = gd1.getByFormat('yyyy/MM/dd');

gs.info("Converted date "+formattedDate);

//Expected output:- 2022-04-31

//Actual output with error:- Javascript compiler exception: Java constructor for "com.glide.glideobject.GlideDate" with arguments "string" not found. (<refname>; line 3) in:

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

what you wish to perform by converting the format?

Regards
Ankur

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

Hi,

I just created one fix script, but I couldn't convert the date as I expected.

var date = '2022-APR-31';

var gd1 = new GlideDate(date);
var formattedDate = gd1.getByFormat('yyyy/MM/dd');

gs.info("Converted date "+formattedDate);

//Expected output:- 2022-04-31

//Actual output with error:- Javascript compiler exception: Java constructor for "com.glide.glideobject.GlideDate" with arguments "string" not found. (<refname>; line 3) in:

@RK 

This worked for me; you can incorporate this in your script include

Ensure you give time as well as end of day i.e. 23:59:59

var choosenDate = '12-APR-2022' + ' 23:59:59'; //Get the selected date

var gd1 = new GlideDateTime();

gd1.setDisplayValue(choosenDate, "dd-MMM-yyyy HH:mm:ss");

gs.info(gd1);

var dt = new GlideDate();
dt.setValue(gd1.getDate());

var formattedDate = dt.getByFormat("yyyy/MM/dd");

gs.info(formattedDate);

Output:

find_real_file.png

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

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