Convert seconds to Duration

narendrand
Giga Contributor

Hi Everyone,

I have two date & time fields in form, I have used the duration calculator to find the difference between two date&time field values. The result i am getting from calcsheduleduration is in "seconds" but i have set this answer in another "duration" type field like days, hours format. If anyone come across this scenario please guide me to resolve this issue.

Thanks & Appreciation in Advance,

Cheers,

Narendran.D

1 ACCEPTED SOLUTION

Well, just more math!


if result = 1.753, then you can convert as follows:



var days = parseInt(result,10);


var remainder = result - days;


var dec_hours = remainder * 24;


var hours = parseInt(dec_hours,10);


remainder = dec_hours - hours;


var dec_minutes = remainder * 60;


var minutes = parseInt(dec_minutes,10);


remainder = dec_minutes - minutes;


var dec_seconds = remainder * 60;


var seconds = parseInt(dec_seconds,10);


if (hours < 10) {


    hours = '0' + hours.toString();


}


if (minutes < 10) {


    minutes = '0' + minutes.toString();


}


if (seconds < 10) {


    seconds = '0' + seconds.toString();


}


var new_result = days + ' ' + hours + ':' + minutes + ':' + seconds;


View solution in original post

9 REPLIES 9

I would try to just GlideDateTime instead. there are many functions for calculation date & time.



For example the convert your looking for I guess you could use:


setNumericValue(milliseconds)Sets the datetime to the number of milliseconds since January 1, 1970 00:00:00 GMT.

You can find more info here:http://wiki.servicenow.com/index.php?title=GlideDateTime#gsc.tab=0


The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: GlideDateTime
 


Visit http://docs.servicenow.com for the latest product documentation


It sounds like you might just need to do some debugging. My script has been tested and works. Can you reproduce your script here and give me a sample input?


Mike Allen
Mega Sage

Here is an example:



3.64 subtract(start, end)

Gets the duration difference between two GlideDateTime values.


3.64.1 Input Fields

Parameters:


  • start - (GlideDateTime) The start value.
  • end - (GlideDateTime) The end value.

3.64.2 Output Fields

Returns: (GlideDuration) The duration between the two values.


3.64.3 Example


var gdt1 = new GlideDateTime("2011-08-28 09:00:00"); var gdt2 = new GlideDateTime("2011-08-31 08:00:00"); var dur = new GlideDuration();     var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2 gs.print(dur.getDisplayValue());


Output:



*** Script: 2 Days 23 Hours

Thanks for the response Mike.


sorry, I have forgot to point out one additional info in my requirement, The two date&time field will hold the two different dates, i want to find the difference between those two dates excluding weekends. for this purpose only i have used schedule duration calculator but i am getting the output in seconds so am not able to set this seconds in duration field. can you guide me to convert this second into duration format?



Cheers,


Narendran.D