# xs:duration

Converts $string-var (a string in the duration format) to the duration data type.

If the value of $string-var is the empty sequence, the empty sequence is returned. The empty sequence is a sequence containing zero items (), which is similar to null in SQL.

### Signatures

xs:duration(xs:string $string-var) —> xs:duration

### Arguments

Data Type | Argument | Description
--- | --- | ---
xs:string | $string-var | Represents a string with the duration specified with one of the following formats: P y Y m M d DT h H i M s S -P y Y m M d DT h H i M s S Note: Specifying the number of years, months, days, hours, minutes, or seconds is optional—any of the substrings: yY, mM, dD, hH, iM, or sS do not have to be specified. For example, the following are valid durations: P10Y, P1D, -P11M, -P10Y7D, P2YT5H, or P6YT5H10S.
| - | Duration is a negative amount of time. Note: If - is not specified, duration is a positive amount of time.
P | The start of a duration string. The P must always be specified.
y Y | y—number of years in the duration.
Y—years are specified in the duration.
m M | m—number of months in the duration.
M—months are specified in the duration.
d D | d—number of days in the duration.
D—days are specified in the duration.
T | The start of the time part of the duration string. The T must be specified if any minutes, hours, or seconds (hH, iM, or sS) are specified.
h H | h—number of hours in the duration.
H—years are specified in the duration.
i M | i—number of minutes in the duration.
M—minutes are specified in the duration.
s S | s—number of seconds in the duration. Seconds can be specified up to 7 digits after the decimal place. (Format xx.xxxxxxx)
S—seconds are specified in the duration.

### Returns

Returns a duration of time as a duration value.

### Examples

#### duration with All Components

Invoking duration("P1Y2M4DT9H8M20S") returns a duration value corresponding to 1 year, 2 months, 4 days, 9 hours, 8 minutes, and 20 seconds, as shown in the following example query:

    <result>{xs:duration("P1Y2M4DT9H8M20S")}</result>

The preceding query, generates the following XML result:

    <result>P1Y2M4DT9H8M20S</result>

#### duration with Just Years

Invoking duration("P9Y") returns a duration value corresponding to 9 years, as shown in the following example query:

    <result>{xs:duration("P9Y")}</result>

The preceding query, generates the following XML result:

    <result>P9Y</result>

#### duration with Just Negative Months

Invoking duration("-P10M") returns a duration value corresponding to negative 10 months as shown in the following example query:

    <result>{xs:duration("-P10M")}</result>

The preceding query, generates the following XML result:

    <result>-P10M</result>

#### duration with Just Days and Seconds

Invoking duration("P4DT20S") returns a duration value corresponding to 4 days and 20 seconds, as shown in the following example query:

    <result>{xs:duration("P4DT20S")}</result>

The preceding query, generates the following XML result:

    <result>P4DT20S</result>

Related Topics

W3C duration data type description