Date format change issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 03:23 AM
Hi Experts,
I have wrote below script to change format of one date field, Code is working and every time it is taking today's date but not which I am passing that date
var gdt = new GlideDateTime(this.getParameter("sysparm_existing"));
gs.log("first date" + gdt);
var myDate = gdt.getDate();
var gd = new GlideDate();
gd = new GlideDateTime(gd.getByFormat("yyyy-MM-dd"));
gs.log("converted date" + gd);
gd.addDays(7);
gs.log("added date" + gd);
return gd.getDate();
Please help me
Thanks,
Atchamma D
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 03:37 AM
Hi @Atchamma D ,
Refer below code:-
GoodWill,
Anshul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 03:40 AM
Hi Atchamma,
Can you try to update this line :
var gdt = new GlideDateTime(this.getParameter("sysparm_existing"));
gs.log("first date" + gdt);
var myDate = gdt.getDate();
var gd = new GlideDate();
gd = new GlideDateTime(gd.getByFormat("yyyy-MM-dd"));
gs.log("converted date" + gd);
gd.addDaysLocalTime(7);
gs.log("added date" + gd);
return gd.getDate();
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 04:42 AM - edited 09-05-2023 04:44 AM
Hello @Atchamma D
It seems like you want to take a date string passed as a parameter, convert it to a GlideDateTime object, add 7 days to it, and then return the resulting date. However, there are some issues with your script.
Firstly, you are using this.getParameter("sysparm_existing") to get the date value, but it seems like you want to use the date you passed into the script instead. You should use a different variable to store the input date.
Secondly, you are initializing a GlideDate object (gd) and then immediately overwriting it with a new GlideDateTime object. This doesn't seem necessary. You can directly work with the gdt object.
Here's a modified version of your script:
// Get the date parameter you passed into the script
var inputDateStr = this.getParameter("sysparm_existing");
// Create a GlideDateTime object from the input date
var gdt = new GlideDateTime(inputDateStr);
gs.log("input date: " + gdt);
// Add 7 days to the GlideDateTime object
gdt.addDays(7);
gs.log("new date after adding 7 days: " + gdt);
// Return the resulting date in yyyy-MM-dd format
return gdt.getDate().toString();
This script should take the input date you passed and add 7 days to it, and then return the resulting date in the "yyyy-MM-dd" format.
Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.
Thanks & Regards,
Kartik Magadum