- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2021 03:55 AM
I have one duration field so I converted it to seconds using attribute set to seconds. This value is milliseconds and so to get value I used: (time_left is the duration field value)
var Duration = (current.time_left.getGlideObject().getNumericValue() / 1000); // multiple by .001 or divide by 1000
Now In duration I have days, hours, minutes in complete seconds .
So I have date field now which needs to be converted to seconds again: on_hold_till is the date field value
I have this code:
var gdt = new GlideDateTime();
gdt.setValue(current.getValue('on_hold_till'));
var ms = gdt.getNumerciValue();
Now in var Duration I have duration in seconds and now in Var ms I have date field in seconds.
So now I have to add this and set this to a new field. ( I want duration to add to a date field to get a new date field):
like adding both this seconds and then converting totals seconds to a date field.
Any help on this!!!!! Please
If you have easy way to add duration to date feel free to provide using the above field values.
TIA
Solved! Go to Solution.
- Labels:
-
Team Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2021 10:26 PM
Hi,
do this
// to get duration time to seconds.
var startTime = new GlideDateTime(current.u_date.getDisplayValue());
var duration = current.time_left.dateNumericValue();
startTime.add(duration);
current.u_target_date = startTime.getDate();
// OR if u_target_date is of type date/time then do this
current.u_target_date = startTime.getDisplayValue();
current.update();
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2021 09:06 AM
The following should work for you, without the need to convert to seconds;
var duration = new GlideDateTime(current.duration);
var on_hold_til = new GlideDateTime(current.on_hold_til);
on_hold_til.add(duration.getNumericValue());
current.NEW_DATE_FIELD = on_hold_til;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2021 09:15 AM
// to get duration time to seconds.
var seconds = (current.u_remaining_hours.getGlideObject().getNumericValue()/1000);
gs.addInfoMessage('duration seconds is' + seconds);
// to change date field to seconds
var gdt = new GlideDateTime();
gdt.setValue(current.getValue('u_date'));
var ms = gdt.getNumericValue()/1000;
gs.addInfoMessage('seconds is' + ms );
// add both seconds field.
var value = seconds + ms;
gs.addInfoMessage('total time' + value);
// convert total seconds to date field.
var dt = new GlideDateTime("1970-01-01 00:00:00");
dt.addSeconds(value);
gs.info(dt.getValue());
//dt.addSeconds(value);
current.u_target_date = dt;
current.update();
This code worked fine when I tried in PDI as I used date and duration fields in incident table but when I am trying this in my office instance I have date field in case table form but duration field in task_sla table. I am writing BR with case table. Is this the reason my BR is not working or what should I do?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2021 10:46 AM
// to get duration time to seconds.
var seconds = (current.time_left.getGlideObject().getNumericValue()/1000);
gs.addInfoMessage('duration seconds is' + seconds);
// to change date field to seconds
var gdt = new GlideDateTime();
gdt.setValue(current.getValue('u_date'));
var ms = gdt.getNumericValue()/1000;
gs.addInfoMessage('seconds is' + ms );
// add both seconds field.
var value = seconds + ms;
gs.addInfoMessage('total time' + value);
// convert total seconds to date field.
var dt = new GlideDateTime("1970-01-01 00:00:00");
dt.addSeconds(value);
gs.info(dt.getValue());
//dt.addSeconds(value);
current.u_target_date = dt;
current.update();
This is getting Date field to seconds value but when it comes to duration it is giving "Nan" so total time is also getting Nan
Error Message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2021 10:26 PM
Hi,
do this
// to get duration time to seconds.
var startTime = new GlideDateTime(current.u_date.getDisplayValue());
var duration = current.time_left.dateNumericValue();
startTime.add(duration);
current.u_target_date = startTime.getDate();
// OR if u_target_date is of type date/time then do this
current.u_target_date = startTime.getDisplayValue();
current.update();
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader