Date format change issue

Atchamma D
Tera Contributor

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

3 REPLIES 3

anshul_goyal
Kilo Sage

Hi @Atchamma D ,

Refer below code:- 

var gdt = new GlideDateTime("03-09-2023");
gs.log("first date" + gdt);
var myDate = gdt.getDate();
var gd = new GlideDateTime()
gdt.getByFormat("yyyy-MM-dd");
gs.print("converted date" + gdt);
gdt.addDays(7);
gs.print("added date" + gdt);
 
Output - 
Anshul21_0-1693910232433.png

GoodWill,
Anshul

Vishal Birajdar
Giga Sage

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();
Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Kartik Magadum
Kilo Sage

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