I'm not sure if this helps, but here is something similar using a technique 
like the functx function does:

http://www.xqueryfunctions.com/xq/functx_yearmonthduration.html


xquery version "1.0-ml";

let $date1 := xs:dateTime(xs:date("2015-03-27"))
let $date2 := xs:dateTime(xs:date("2024-03-27"))
let $test :=  xs:duration($date2 - $date1)
let $years := fn:round(fn:days-from-duration($test) div 365)
let $months := fn:round((fn:days-from-duration($test) div 365) div 12)
return
 ( xs:yearMonthDuration('P1M') * $months +
    xs:yearMonthDuration('P1Y') * $years)

But duration math is strange so I might be doing something wrong.

-Danny


-----Original Message-----
From: general-boun...@developer.marklogic.com 
[mailto:general-boun...@developer.marklogic.com] On Behalf Of Florent Georges
Sent: Thursday, February 19, 2015 4:26 PM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] xs:yearMonthDuration in 1.0-ml

  That function never found its way from the working drafts to the 
recommendation.  You can achieve something similar by using the following (yes, 
by using the average number of days in a month, which is fragile and works fine 
only for large numbers of days, over several
years):

    let $days   := xs:dayTimeDuration("-P3278D") div xs:dayTimeDuration('P1D')
    let $months := $days idiv 30.43684991666667
    return
      $months * xs:yearMonthDuration('P1M')

  Regards,

--
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/


On 20 February 2015 at 01:02, William Sawyer wrote:
> I have two dates I am trying to subtract and get a xs:yearMonthDuration.  In
> "0.9-ml" there is a function you can call but in "1.0-ml" the documentation
> says to subtract the dates.  But how do I specify that I want a
> xs:yearMonthDuration instead of a xs:dayTimeDuration.
> MarkLogic Version: 7.0-2.3
>
> Thanks,
> -Will
>
>
> xquery version "1.0-ml";
>
> let $date1 := xs:dateTime(xs:date("2015-03-27"))
> let $date2 := xs:dateTime(xs:date("2024-03-27"))
> let $test := $date1 - $date2
> return $test
> ***************
> xs:dayTimeDuration("-P3288D")
>
>
> xquery version "0.9-ml"
>
> let $date1 := xs:dateTime(xs:date("2015-03-27"))
> let $date2 := xs:dateTime(xs:date("2024-03-27"))
> let $test := fn:subtract-dateTimes-yielding-yearMonthDuration($date1,
> $date2)
> return $test
> **************
> xs:yearMonthDuration("-P8Y11M")
>
> _______________________________________________
> General mailing list
> General@developer.marklogic.com
> http://developer.marklogic.com/mailman/listinfo/general
>

-- 
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/
_______________________________________________
General mailing list
General@developer.marklogic.com
http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
General@developer.marklogic.com
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to