> For the complete documentation index, see [llms.txt](https://docs.wem.io/platform/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.wem.io/platform/tutorials/building-widgets/wemscript/8-types/5-duration.md).

# 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'
?>
```

#### 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.

| 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wem.io/platform/tutorials/building-widgets/wemscript/8-types/5-duration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
