fn:months-from-duration function

The fn:months-from-duration function returns the months component of a duration value.

Syntax

Read syntax diagramSkip visual syntax diagramfn:months-from-duration( duration-value)
duration-value
The duration value from which the months component is to be extracted.

duration-value is an empty sequence, or is a value that has one of the following types: xs:dayTimeDuration, xs:duration, or xs:yearMonthDuration.

Returned value

The return value depends on the type of duration-value:

  • If duration-value is of type xs:duration or is of type xs:yearMonthDuration, the returned value is of type xs:integer, and is a value is between -11 and 11, inclusive. The value is the months component of duration-value cast as xs:yearMonthDuration. The value is negative if duration-value is negative.
  • If duration-value is of type xs:dayTimeDuration, the returned value is 0.
  • If duration-value is an empty sequence, the returned value is an empty sequence.

The months component of duration-value cast as xs:yearMonthDuration is the integer number of months remaining from the total number of months of duration-value divided by 12.

Examples

The following function returns the months component of the duration 20 years and 5 months.

fn:months-from-duration(xs:duration("P20Y5M"))

The returned value is 5.

The following function returns the months component of the yearMonthDuration -9 years and -13 months.

fn:months-from-duration(xs:yearMonthDuration("-P9Y13M"))

The returned value is -1. When calculating the total number of months in the duration, -13 months is converted to -1 year and -1 month. The duration is equal to -P10Y1M which has a month component of -1 month.

The following function returns the months component of the duration 14 years, 11 months, 40 days, and 13 hours.

xquery fn:months-from-duration(xs:duration("P14Y11M40DT13H"))

The returned value is 11.