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

So Rahuljain, the code is definitely working but now the customer is stating that they want to be able to modify the date. It must start at 5 days out but they want to be able to modify it so if today's date is 1/12/16 - the date should start at 1/17/16 but the customer should be able to select a different date in the future as long as it is after the 17th if that makes sense.


Karen,


You may want to go at this a different route.   Allow them to pick a date and if the date is not at least 5 days out prompt them with a message box stating so before they submit the request. This way they will either set the date to 5 days out, or pick what ever the number of days out they want as long as it is past 5. All you will need to do is confirm it is 5 days from the current date.



Tom


Yes I am also thinking the same approach. I did something similar for an onboarding form with an alert box validating if they are sure that they want that value.


Karen, we had the same issue.   Doing the same thing you are.   Here is the code we used:


var now = new GlideDateTime();
var myDate;
var copy = new Date();
var currentTime = copy.getHours()+ ':' + copy.getMinutes() + ':' + copy.getSeconds();


//We need to set the due date 4 business days out.
//Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5


if (now.getDayOfWeek() == 1)
{
myDate = new GlideDateTime(gs.daysAgo(-4)).toString().split(' ')[0] + ' ' + currentTime;
}



The problem with this code, however, is that the current time is the time from where ever the server is running from.   So I changed current time to gs.nowdatetime.



Then we split the date and time and appended the time on to the date that was 4 days out.


So now we do this:


var now = new GlideDateTime();
var myDate;
var copy = new Date();
var currentTime = gs.nowDateTime();


//We need to set the due date 4 business days out.
//Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5


if (now.getDayOfWeek() == 1) //Monday
{
myDate = new GlideDateTime(gs.daysAgo(-4)).toString().split(' ')[0] + ' ' + currentTime.toString().split(' ')[1];
}



Tom


Mani I am thinking something like: var myDate=new Date(); myDate.now.format(myDate.setDate(myDate.getDate()+5),("dd/mm/yyyy");