> For the complete documentation index, see [llms.txt](https://docs.b2winsuite.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.b2winsuite.com/5.2/scripting/static-classes/duration-class.md).

# Duration Class

The `Duration` class is designed to represent a time-based duration, typically measured in seconds and nanoseconds. It offers a range of static methods for creating durations based on days, hours, minutes, seconds, milliseconds, and nanoseconds. Additionally, it provides conversion methods to express the duration in various units, as well as the ability to calculate the duration between two temporal objects. This class is ideal for measuring time spans, tracking elapsed time, and performing time-based calculations in applications.

### Static Methods

#### `ofDays(days: Long)`

* Obtains a `Duration` representing a number of standard 24-hour days.
* Example Usage:

  ```java
  var daysDuration = Duration.ofDays(5);
  ```

#### `ofHours(hours: Long)`

* Obtains a `Duration` representing a number of standard hours.
* Example Usage:

  ```java
  var hoursDuration = Duration.ofHours(12);
  ```

#### `ofMinutes(minutes: Long)`

* Obtains a `Duration` representing a number of standard minutes.
* Example Usage:

  ```java
  var minutesDuration = Duration.ofMinutes(30);
  ```

#### `ofSeconds(seconds: Long)`

* Obtains a `Duration` representing a number of seconds.
* Example Usage:

  ```java
  var secondsDuration = Duration.ofSeconds(120);
  ```

#### `ofMillis(millis: Long)`

* Obtains a `Duration` representing a number of milliseconds.
* Example Usage:

  ```java
  var millisDuration = Duration.ofMillis(5000);
  ```

#### `ofNanos(nanos: Long)`

* Obtains a `Duration` representing a number of nanoseconds.
* Example Usage:

  ```java
  var nanosDuration = Duration.ofNanos(123456789);
  ```

#### `parse(text: String)`

* Obtains a `Duration` from a text string such as `PnDTnHnMn.nS`.
* Parses a textual representation of a duration, including the string produced by `toString()`.
* Example Usage:

  ```java
  var parsedDuration = Duration.parse("PT20.345S");
  ```

### Conversion Methods

#### `toString()`

* A string representation of this duration using ISO-8601 seconds based representation, such as `PT8H6M12.345S`.
* Example Usage:

  ```java
  var stringRepresentation = parsedDuration.toString();
  ```

#### `toDays()`

* Gets the number of days in this duration.
* Example Usage:

  ```java
  var days = daysDuration.toDays();
  ```

#### `toHours()`

* Gets the number of hours in this duration.
* Example Usage:

  ```java
  var hours = hoursDuration.toHours();
  ```

#### `toMinutes()`

* Gets the number of minutes in this duration.
* Example Usage:

  ```java
  var minutes = minutesDuration.toMinutes();
  ```

#### `toSeconds()`

* Gets the number of seconds in this duration.
* Example Usage:

  ```java
  var seconds = secondsDuration.toSeconds();
  ```

#### `toMillis()`

* Converts this duration to the total length in milliseconds.
* Example Usage:

  ```java
  var millis = millisDuration.toMillis();
  ```

#### `toNanos()`

* Converts this duration to the total length in nanoseconds expressed as a `long`.
* Example Usage:

  ```java
  var nanos = nanosDuration.toNanos();
  ```

#### `between(startInclusive: any, endExclusive: any)`

* Obtains a `Duration` representing the duration between two temporal objects.
* Example Usage:

  ```java
  var start = LocalDateTime.now(); // current date-time
  var end = start.plusHours(2); // current date-time plus 2 hours
  var duration = Duration.between(start, end); // duration is 2 hours
  ```

### **Examples**

**Example: Days Ago**

**Description:** This script calculates the number of days between a specified ‘order\_date’ and the current date.

**Use Case:** Useful for tracking the time elapsed since an order was placed.

```javascript
var days = Duration.between(order_date.atStartOfDay(), 
                                LocalDate.now().atStartOfDay());

//Return the days as a number, we can also return toMinutes(), toYears(), ... as well
return days.toDays();
```

This example demonstrates how the Duration class can be used for tracking the age of data or calculating time-based metrics in various applications.


---

# 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, and the optional `goal` query parameter:

```
GET https://docs.b2winsuite.com/5.2/scripting/static-classes/duration-class.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
