I am adding 14 days to current date and setting into date type field?

Shantharao
Kilo Sage

Hello All,

I am adding 14 days to current date type filed and setting into date type field? 

I have written below code for adding 14 days to current date and adding into date type variable as below

 

Client Script

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('Request_AjaxUtil');
ga.addParam('sysparm_name', 'setNeedByDate');
ga.getXML(setDate);

function setDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue("need_by_date", answer);
}
}

 

Script Include Function

setNeedByDate: function() {

var currentDate = new GlideDateTime();
currentDate.addDaysLocalTime(14);
gs.addInfoMessage("14 days " + currentDate.getDate());
return currentDate.getDate();
},

Output : "2020-02-13" (today's output) but I want change this format to "13-Feb-20" 

How can I change this "2020-02-13" format to "13-Feb-20"

 

 

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

you can use below script 

Sample code:

 

var gd = new GlideDate(); 
gd.setValue('2015-01-01');
gs.info(gd.getByFormat("dd-MMM-yyyy"));

 

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_ScopedGlideDateGetByFormat_String

 

script Include:

 

var currentDate = new GlideDateTime();
currentDate.addDaysLocalTime(14);

gs.print(currentDate.getDate().getByFormat("dd-MMM-yyyy"));

 

 

If my answer helped you , kindly mark the answer correct and helpful.

View solution in original post

3 REPLIES 3

Harsh Vardhan
Giga Patron

you can use below script 

Sample code:

 

var gd = new GlideDate(); 
gd.setValue('2015-01-01');
gs.info(gd.getByFormat("dd-MMM-yyyy"));

 

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_ScopedGlideDateGetByFormat_String

 

script Include:

 

var currentDate = new GlideDateTime();
currentDate.addDaysLocalTime(14);

gs.print(currentDate.getDate().getByFormat("dd-MMM-yyyy"));

 

 

If my answer helped you , kindly mark the answer correct and helpful.

Hi Harsha,

 

As per the above code date variable is setting with proper format but when I try to submit the request it is showing below errro

 

find_real_file.png

Dhananjay Pawar
Kilo Sage

Hi,

You can also set default value to date field by adding days.Check below link it will help you.

https://community.servicenow.com/community?id=community_question&sys_id=d5d7432ddb1cdbc01dcaf3231f96...

Mark correct/helpful based on impact.

Thanks,

Dhananjay.