Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Schedule Job Get Date Value

Jeff42
Tera Contributor

Hi all,

I have a problem on Date field calculation, could any one give me suggestions?

 

1. I have Contract End Date in  the Tabble

Jeff42_0-1702559283368.png

2. Create Scheduled Jobs with Script

var gr = new GlideRecord('sn_hr_core_contract_management');
gr.addEncodedQuery('u_contract_end_dateISNOTEMPTY');
gr.query();
while (gr.next()) {

    var contract_end_date = new Date(gr.u_contract_end_date.getDisplayValue());
    gs.info('Contract End' + contract_end_date);
    var today = new Date();
    gs.info('Today ' + today);
    var oneDay = 24 * 60 * 60 * 1000;
    var diffDays = Math.round(Math.abs((contract_end_date - today) / oneDay));
    gs.info('HRIS Test ' + diffDays);
}
3. below result: you can find that the contract end is not correct with the table value
Jeff42_1-1702559458716.png

 

Thank you

Best Regards

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Jeff42 

so basically you want difference between contract end date and today's date in days?

if yes then use this

var gr = new GlideRecord('sn_hr_core_contract_management');
gr.addEncodedQuery('u_contract_end_dateISNOTEMPTY');
gr.query();
while (gr.next()) {

	var contract_end_date = new GlideDateTime(gr.u_contract_end_date);
	var today = new GlideDateTime();

	var dur = new GlideDuration();
	dur = GlideDateTime.subtract(today, contract_end_date);

	var days = dur.getDayPart();
	gs.info("HRIS Test" + days);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Jeff42 

so basically you want difference between contract end date and today's date in days?

if yes then use this

var gr = new GlideRecord('sn_hr_core_contract_management');
gr.addEncodedQuery('u_contract_end_dateISNOTEMPTY');
gr.query();
while (gr.next()) {

	var contract_end_date = new GlideDateTime(gr.u_contract_end_date);
	var today = new GlideDateTime();

	var dur = new GlideDuration();
	dur = GlideDateTime.subtract(today, contract_end_date);

	var days = dur.getDayPart();
	gs.info("HRIS Test" + days);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Working now, thanks so much.