【问题标题】:Get first day and last day dates of month of selected date in Marklogic在 Marklogic 中获取所选日期月份的第一天和最后一天日期
【发布时间】:2018-09-03 10:48:22
【问题描述】:

我正在尝试使用 MarkLogic 获取任何选定日期的第一天和最后一天的日期。
示例:

let $date := xs:date("2018-08-24")      
let $firstDay := "*Should be 1st day of month - of $date*"  
let $lastDay := "*Should be last day of month - of $date*"  
return ("$firstDay:",$firstDay,"$lastDay:",$lastDay)

预计输出日期:

  • $firstDay: 2018-08-01
  • $lastDay: 2018-08-31

我可以通过使用 functx:first-day-of-month($date)functx:last-day-of-month($date) 但我想知道 MarkLogic

是否提供任何 API 或替代选项

【问题讨论】:

    标签: date xquery marklogic marklogic-8


    【解决方案1】:

    您可以使用标准 XQuery duration arithmetic 直接计算:

    let $date := xs:date("2018-08-24")
    
    let $one-day := xs:dayTimeDuration('P1D')
    let $one-month := xs:yearMonthDuration('P1M')
    (: subtract days to get to the 1st of the month :)
    let $firstDay := $date - (fn:day-from-date($date) - 1) * $one-day
    (: get the 1st of the next month and then subtract one day :)
    let $lastDay  := $firstDay + $one-month - $one-day
    
    return ("$firstDay:",$firstDay,"$lastDay:",$lastDay)
    

    【讨论】:

    • 这很有用。我们没有MarkLogic提供的现成API吗?
    • functx 可作为 MarkLogic 中的库模块使用:import module namespace functx = "http://www.functx.com" at "/MarkLogic/functx/functx-1.0-nodoc-2007-01.xqy";
    • 是的,我们可以使用 functx 函数,但我正在检查而不是导入外部函数 (functx) 并在 ML 中使用,难道我们在 MarkLogic 本身中有一些东西吗?早些时候我使用的是functx,但现在我有一个内部算术持续时间计算函数,正如@Leo Wörteler 在上面的答案中所建议的那样。谢谢。
    • 我还看到了一种使用子字符串函数计算任何选定日期的第一个日期的简单方法。 xs:date(fn:concat(fn:substring(xs:string($date),1,8), "01"))
    猜你喜欢
    • 1970-01-01
    • 2010-10-10
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多