Search code examples
xquerymarklogicmarklogic-10

Need to return months in-between dates in XQuery


I'm looking for return months in-between dates,

  • Start date: 2022-03-17
  • End Date: 2022-06-17

I am able to get days through the below query.

days-from-duration(xs:date('2022-03-17') - xs:date('2022-06-17')) 

But my expected output is: 2022-03,2022-04,2022-05,2022-06


Solution

  • For each of the days in the fn:days-from-duration(), you can add those days to the start-date and then use fn:format-date() to generate the YYYY-MM value, and then de-dup with distinct-values().

    let $start := xs:date('2022-03-17')
    let $end := xs:date('2022-06-17')
    let $year-months := 
      for $day in 1 to days-from-duration($end - $start ) 
      let $date := $start + xs:dayTimeDuration("P"||$day||"D") 
      return fn:format-date($date, "[Y0001]-[M01]")
    return
      distinct-values($year-months)