declare function local:addZero($int as xs:int) as xs:string { let $string := xs:string($int) return if (string-length($string) = 2) then $string else string-join(('0',$string),'') } declare function local:addDays($date as xs:date, $days as xs:int) as xs:date { let $year := fn:year-from-date($date), $longYear := $year mod 4 = 0 and not($year mod 100 = 0 and not($year mod 400 = 0)), $month := fn:month-from-date($date), $day := fn:day-from-date($date), $daysInMonth := if ($month = (1,3,5,7,8,10,12)) then 31 else if ($month = 2) then if ($longYear) then 29 else 28 else 30, $daysLeftInMonth := $daysInMonth - $day, $newDay := if ($days >= $daysLeftInMonth) then $day + $days - $daysInMonth else $day + $days, $newMonth := if ($days >= $daysLeftInMonth) then if ($month = 12) then 1 else $month + 1 else $month, $newYear := if ($days >= $daysLeftInMonth and $month = 12) then $year + 1 else $year return (xs:date(string-join((string($newYear),local:addZero($newMonth),local:addZero($newDay)),'-'))) } local:addDays(current-date(),10)