5. Duration
A duration
type represents a measure of the interval between two events or points in time in a platform-independent format.
Print Output Overview
Let's assume we declared var @d: duration
, which holds a duration of '1d23h45m6s'
—representing 1 day, 23 hours, 45 minutes, and 6 seconds.
<?= @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:
Depending on the situation, you may want to use Math.round()
on the numbers.
Text Field Postback Example
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'
:
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?