g_form getValue Duration

stevejarman
Giga Guru

Can anyone tell me how to get the value from a Duration field in a useful format? I need to be able to grab the Duration field value and minus it from a Date/Time value (which I already have). I'm totally stumped at this point.

1 ACCEPTED SOLUTION

I was talking about reading the value. All good though - I've just written a small function to convert the value it returns to a millisecond value, which is much more useable. Maybe there's a built-in way to do it, but if there is, I couldn't find it.



function GlideFormDurationToMS(_duration)


{


      var totalMS = 0;



      var dayTimeSplit = _duration.split(" ");



      totalMS += Number(dayTimeSplit[0]) * 86400000; // DAYS



      var timeSplit = dayTimeSplit[1].split(":");



      totalMS += Number(timeSplit[0]) * 3600000; // HOURS


      totalMS += Number(timeSplit[1]) * 60000; // MINUTES


      totalMS += Number(timeSplit[2]) * 1000; // SECONDS



      return totalMS;


}


View solution in original post

6 REPLIES 6

I found a simpler way to do this. I couldn't get the function above to work for me, so I used the following script to grab the numeric value of the duration so that I could then manipulate that value. I found the solution on Convert Duration to Minutes as Integer



var actualDuration = current.u_actual_downtime.getGlideObject().getNumericValue();


// gs.addInfoMessage('Actual duration numeric value: '+ actualDuration);


var actualDurationMinutes = (actualDuration/1000/60);


// gs.addInfoMessage('Actual duration in minutes value: '+ actualDurationMinutes);


var scheduledDuration = current.u_scheduled_downtime.getGlideObject().getNumericValue();


// gs.addInfoMessage('Scheduled duration numeric value: '+ scheduledDuration);


var scheduledDurationMinutes = (scheduledDuration/1000/60);


// gs.addInfoMessage('Scheduled duration in minutes value: '+ scheduledDurationMinutes);




if(current.u_actual_downtime !=''){


current.u_minutes_downtime = actualDurationMinutes.toFixed();


current.update();


}


else {


current.u_minutes_downtime = scheduledDurationMinutes.toFixed();


current.update();


}


Cristiano3
Giga Contributor

Hi everyone.

I know it's been a while since the last time someone posted answers here, but I'd like to contribute with my 2 cents, 'cause this thread has helped me.

I've made a script include based on the codes I've seen here and I'd like to share it:

var GlideDurationToNumber = Class.create();
GlideDurationToNumber.prototype = {
	initialize: function() {
	},

	toMS: function(duration) {
		var durationValue = duration.getDurationValue();
		var totalMS = 0;
		var dayTimeSplit, timeSplit;

		if(durationValue.indexOf(' ') != -1) {
			dayTimeSplit = durationValue.split(' ');
			totalMS += Number(dayTimeSplit[0]) * 24 * 60 * 60 * 1000; // DAYS → MS

			timeSplit = dayTimeSplit[1].split(':');
		}
		else {
			timeSplit = durationValue.split(':');
		}

		totalMS += Number(timeSplit[0]) * 60 * 60 * 1000; // HOURS → MS
		totalMS += Number(timeSplit[1]) * 60 * 1000; // MINUTES → MS
		totalMS += Number(timeSplit[2]) * 1000; // SECONDS → MS

		return totalMS;
	},

	toSec: function(duration) {
		return this.toMS(duration) / 1000;
	},

	toMin: function(duration) {
		return this.toMS(duration) / 1000 / 60;
	},

	toHour: function(duration) {
		return this.toMS(duration) / 1000 / 60 / 60;
	},

	toDay: function(duration) {
		return this.toMS(duration) / 1000 / 60 / 60 / 24;
	},

	type: 'GlideDurationToNumber'
};

Here's an example of how to use it:

var dur = new GlideDuration('3 15:30:00');
var util = new GlideDurationToNumber();

gs.print(util.toMS(dur) + ' MS');
gs.print(util.toSec(dur) + ' Seconds');
gs.print(util.toMin(dur) + ' Minutes');
gs.print(util.toHour(dur) + ' Hours');
gs.print(util.toDay(dur).toFixed(2) + ' Days');

Output:

315000000 MS
315000 Seconds
5250 Minutes
87.5 Hours
3.65 Days

I hope this is useful for someone.

Cheers,
Cristiano.