Formatter.Duration (Duration Formatter)
The Duration Formatter is used to display time-based data.
Duration Formatter Constructor
Here is the constructor (function) of the Formater.Duration class:
new Formatter.Duration() (--> Formatter.Duration) • Creates a new instance of the Formatter.Duration class.
Duration Formatter Instance Properties
Here are the properties of an instance of the Formatter.Duration class:
hoursPerDay (--> Number) • The number of hours considered to comprise a “work day” (In the U.S. the default is 8).
hoursPerWeek (--> Number) • The number of hours considered to comprise a “work week” (In the U.S. the default is 40).
useVerboseFormat (--> Boolean) • If true, the formatter spells out the time durations; for example, 1d 4h gets expanded to 1 day 4 hours.
Duration Formatter Instance Methods (functions)
Here are the methods (functions) of an instance of the Formater.Duration class:
stringFromDecimal(number:Decimal) (--> String or nil) • Generates a corresponding string for the provided decimal value.
decimalFromString(string:String) (--> Decimal or nil) • Generates a corresponding decimal value for the provided string.
Total Hours as Duration
dVal = Decimal.fromString(String(123.52))fmtr = new Formatter.Duration()fmtr.useVerboseFormat = truefmtr.stringFromDecimal(dVal)//--> "3 weeks 3.52 hours"
Total Hours of Duration
fmtr = new Formatter.Duration()dVal = fmtr.decimalFromString("3w 3d 3h")totalHours = Number(dVal.toString())//--> 147
Calculations
The Decimal class has a set of functions for performing calculations with decimal instances, that can be used with to sum multiple durations:
Calculating Total of Multiple Durations
durationA = "2w 3d 1.5h"durationB = "3w 4d 6.25h"durationC = "1w 3d 4.25h"durations = [durationA, durationB, durationC]fmtr = new Formatter.Duration()totalTime = Decimal.zerodurations.forEach(duration => {totalTime = totalTime.add(fmtr.decimalFromString(duration))})fmtr.useVerboseFormat = truefmtr.stringFromDecimal(totalTime)//--> "8 weeks 1 day 4 hours"