Change the date format using server script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2022 01:26 AM
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,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2022 03:51 AM
Hi,
Can you remove the toString() from the line and try
var choosenDate = this.getParameter('sysparm_date').toString();
Regards,
Deepankar Mathur
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2022 04:24 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2022 03:25 AM
Hi,
what you wish to perform by converting the format?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2022 04:26 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2022 05:06 AM
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:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader