How to Calculate from Current Time and Date

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2019 05:13 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2019 07:00 PM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2019 12:33 PM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2019 01:38 PM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2019 02:38 PM
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?