Add days to datetime field

neet1
Giga Explorer

Looking to populate end date with 14 days after the value which is there in the start date (on field change)

 

Should respect the user date format preference

3 REPLIES 3

Chalan B L
Giga Guru

var gdt = new GlideDateTime(gs.nowDateTime());

 task.start_date = gdt;

 var ed = new GlideDateTime(gdt);

 ed.addDays(14);

 task.end_date = ed;

 

I ran a background script and produced the results you'd expect.

 

var gdt = new GlideDateTime(gs.nowDateTime());

 gs.log(gdt);

 var ed = new GlideDateTime(gdt);

 ed.addDays(14);

 gs.log(ed);

 

find_real_file.png

 

Yes it will  respect the user date format preference

 

Mark the answer as correct and helpful if it helped you..!!!

 

Regards,

Chalan

Ravi T
Tera Guru

Hi

Find the below thread which will guide to the solution

 

https://community.servicenow.com/community?id=community_question&sys_id=8b48cbaddb1cdbc01dcaf3231f961928&view_source=searchResult

 

Mark it correct/helpful if it helps you.

Regards

Ravindra

Palash Sarkar1
Giga Expert

Hi Neet

Hope you already resolved your query.

Recently I also looked for same answer and I try below script for the date time convert. Hope it will help to others.

 

var gr = new GlideRecord('sys_user');

gr.addEncodedQuery("user_name=XXX10101");
gr.query();

gs.print("Count:"+gr.getRowCount());


while (gr.next())
{

//Get user birthday date
var ubd = gr.u_date_birthday;
var fixBD = new GlideDateTime(ubd);
fixBD.addDays(14);

//Convert date format date

var gDate = fixBD.getDate();
gDate.getByFormat("yyyy-MM-dd");
gr.u_date_birthday= gDate;
gr.update();

}

 

 

Reference link for "GlideDateTime"

https://developer.servicenow.com/print_page.do?release=paris&category=null&identifier=c_APIRef&module=api

Reference link for "getByFormat"

https://developer.servicenow.com/print_page.do?release=paris&category=null&identifier=c_APIRef&module=api