How to Calculate from Current Time and Date

Beto
Mega Guru

How do I calculate from current time/date to Go-Live date and output to Days Remaining?

Go-Live Date: u_go_live_date

Days Remaining: u_days_remaining

 

find_real_file.png

5 REPLIES 5

Allen Andreas
Administrator
Administrator

Hi,

You can use dateDiff to calculate the difference then set that to the duration field.

Please see link for example: https://community.servicenow.com/community?id=community_question&sys_id=0da20f21dbd8dbc01dcaf3231f96...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

I created script for calculation with the dictionary for u_days_remaining. Does not calculating correctly. Maybe its better to use a client script?

 

(function calculatedFieldValue(current) {

	var startDate = new GlideDateTime(); // replace with your variable name
	

	var endDate = new GlideDateTime(current.u_go_live_date);  // replace with your variable name
	

	var result = new GlideDateTime.subtract(startDate, endDate);
	
	
return result;


})(current);

Hi,

Not sure what you mean by it's not calculating correctly...can you elaborate a bit more on that?

The code above would work, in the right context. So if it's not working in the default value field, I would imagine you'd need to create a script include, with this in it (since I believe GlideDateTime isn't available in client script) and then reference your script include in an onChange client script for when the end date is touched.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

I believe it has something to do with the time zone. It may have to do with outputting the time 7 hours ahead (We are PST). For example, the Test Field is just a datetime output. It is suppose to say 2019-06-27 14:29:27

 

So the go live date field is passed 10 days ago, but it is strangely showing 17 Hours 30 minutes. Shouldn't it show 0?

 

find_real_file.png