Want a date field to be 5 days out from the date the request was opened?

Cupcake
Mega Guru

I have a variable that is a date field. I want the date field to be 5 days out from the date the request was opened?

I've reviewed a SN Wiki article for Date/Time scripts, but after extensive reading of that article it didn't seem like any of the solutions worked so just trying to get some assistance.

Thanks,

Karen

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee
ServiceNow Employee

Karen,



The script should be:



var today = new Date();


var dateMS = today.getTime();


dateMS = dateMS + (5*24*60*60*1000);


var newDT = new Date();


newDT.setTime(dateMS);


g_form.setValue('ftr_needed_by',formatDate(newDT,g_user_date_format));


View solution in original post

19 REPLIES 19

Chandresh
Tera Guru

Hi Karen,


Hope below link helps



Scoped GlideDateTime API Reference - ServiceNow Wiki



Check specifcially   4.35 addDaysLocalTime(Number amount) in above link


twright5
Kilo Expert

go to your variable.   Under the default tab add something like



javascript:


u_my_date_field = gs.daysAgo(-4) //where u_my_date_field is the name of the date field you are trying to populate.




In the Wiki go here to find out more: GlideSystem Date and Time Functions - ServiceNow Wiki


Tom this is getting me there. It seems to only be populating the current date no matter what value i put in the gs.daysAgo(-4), etc.


find_real_file.png


find_real_file.png


Rahul Jain11
Kilo Guru

Hi,



You can do this:



var gdt = new GlideDateTime(current.opened_at);


gdt.addDaysUTC(5);


gs.print(gdt.getDate());


Thank you for your input as well.


I enter this a default value inside the variable and the date shows up as the current day's date.



Not really sure what is going on.