Extracting Components of Dates, Times, and Durations

A number of functions allow you to extract specific parts of dates, times, and durations. For example, to retrieve the year part of a date, you can use the year-from-date function. Table 19-5 lists all the component extraction functions for date, time, and duration values.

Table 19-5. Component extraction functions

year-from-dateTime

day-from-dateTime

minutes-from-dateTime

year-from-date

day-from-date

minutes-from-time

years-from-duration

minutes-from-duration

days-from-duration

month-from-dateTime

hours-from-dateTime

seconds-from-dateTime

month-from-date

hours-from-time

seconds-from-time

months-from-duration

hours-from-duration

seconds-from-duration

When working with duration values, these functions calculate the result based on the canonical representation of the values. For example, the function years-from-duration does not necessarily return the integer that appears before the Y in the original value. Rather, it returns the number of whole years in the duration, taking into account that the number of months might be more than 12. If the xs:duration value is P1Y15M, the function returns 2, not 1. This is because the canonical representation of P1Y15M (1 year and 15 months) is actually P2Y3M (2 years and 3 months). To extract the number of years as a decimal number, you could instead divide the value by the duration P1Y, which would return 2.25. This is described in "Dividing Durations by Durations," later in this chapter.

Table 19-6 shows some examples of calls to the component extraction functions.

Table 19-6. Examples of the component extraction functions

Example

Return value

month-from-date(xs:date("1999-05-31"))

5

hours-from-time(xs:time("09:54:00"))

9

seconds-from-duration (xs:duration("P5DT1H13.6S"))

13.6

seconds-from-duration(xs:duration("-P5DT1H13.6S"))

−13.6

years-from-duration (xs:duration("P5Y3M"))

5

years-from-duration (xs:duration("P1Y15M"))

2

If you want to extract the entire date or time from an xs:dateTime value, you can cast that value to the desired type (either xs:date or xs:time).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.46.130