add seconds and convert to date

radt
Kilo Guru

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

 

1 ACCEPTED SOLUTION

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

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

View solution in original post

12 REPLIES 12

Inactive_Us1594
Kilo Contributor

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;

radt
Kilo Guru


// 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?

radt
Kilo Guru

// 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

Error running business rule 'seconds' on incident: INC0010007, exception: org.mozilla.javascript.EvaluatorException: Cannot convert NaN to java.lang.Long (sys_script.5c152d0f07a03010e6e6ff4c7c1ed037.script; line 27)
 
 
27th line is:  dt.addSeconds(value);
 
How to fix this Nan and get a value for duration. Please do help 🙂
 
@Ankur Bawiskar 
 

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

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