5. Duration

A duration type represents a measure of the interval between two events or points in time in a platform-independent format.

<?
    var @d: duration
    
    /* 2 minutes */
    @d := '2m'

    /* 4 hours and 6 seconds */
    @d := '4h6s'

    /* 14 days and 5.5 hours */
    @d := '14d5.5h'
?>

Let's assume we declared var @d: duration, which holds a duration of '1d23h45m6s'—representing 1 day, 23 hours, 45 minutes, and 6 seconds.

Encoding
Output

<?= @d ?>

23:45:06

<?attr @d ?>

23:45:06

<?raw @d ?>

23:45:06

<?js @d ?>

50250000000

Be cautious! As you can see, when printing a variable of type duration, only the time part is displayed, not the date part. Although we specified 1 day in our variable @d, it is not printed.

There is no native "duration" type in JavaScript. However, when printing the variable as a JavaScript number, it is represented as a Number type in ticks, which is very precise. One tick represents 100 nanoseconds, and there are 10,000 ticks in a millisecond. Be careful when converting ticks to other units:

<? startupscript ?>
    const durationInTicks = <?js @DurationProperty ?>;
	const durationInMilliseconds = <?js @DurationProperty ?> / 10_000;
	const durationInSeconds = <?js @DurationProperty ?> / 10_000_000;
	const durationInMinutes = <?js @DurationProperty ?> / 600_000_000;
	const durationInHours = <?js @DurationProperty ?> / 36_000_000_000;
	const durationInDays = <?js @DurationProperty ?> / 864_000_000_000;
<? end ?>

Depending on the situation, you may want to use Math.round() on the numbers.

Text Field Postback Example

<? register input @DurationProperty unit = day ?>
<input type="text" name="<?attr OutputId(@DurationProperty) ?>" value="<?attr FormatDuration(@DurationProperty, "days") ?>">

In the example above, we register input for @DurationProperty with unit = day. This informs the WEM runtime that during a postback, a number representing the number of days will be sent. To convert our duration to days, we use FormatDuration(@DurationProperty, "days"), specifying "days" as the second argument to indicate the desired unit. If we want the input field to hold minutes instead of days, we change it to register input @DurationProperty unit = minute and use FormatDuration(@DurationProperty, "minutes").

There are numerous ways to format a duration. The following outputs are generated when we have a var @d: duration with a value of '12d34h56m1s':

Call
Output
Remarks

FormatDuration(@d, "days")

13

FormatDuration(@d, "hours")

322

FormatDuration(@d, "minutes")

19376

FormatDuration(@d, "seconds")

1162561

FormatDuration(@d, "long")

13 dagen, 10 uur, 56 minuten en 1 seconde

In Dutch language

FormatDuration(@d, en_US)

13:10:56:01

FormatDuration(@d, nl_NL)

13:10:56:01

FormatDuration(@d, "clock")

10:56

FormatDuration(@d, "time")

10:56

FormatDuration(@d, "stopwatch")

10:56:01

Be aware that in some situations, the duration may be truncated. For instance, when using "clock", only the hours and minutes are shown. However, when formatting to "seconds", the entire duration is represented in seconds.

Last updated

Was this helpful?