CAML 查询过滤日期范围内的数据

CAML Query filtering data within a date range

我想从 SPList 检索一个日期范围内的项目。开始日期为今天,结束日期为从今天起 30 天后的第二天。这是我的 CAML 查询。

query.Query = string.Concat(@
    "<Where>
        <And>
          <Geq>
            <FieldRef Name='EventDate' />
            <Value IncludeTimeValue='False' Type='DateTime'><Today /></Value>
          </Geq>
          <Leq>
            <FieldRef Name='EventDate' />
            <Value IncludeTimeValue='False' Type='DateTime'><Today offset='30'/></Value>
          </Leq>
       </And>
     </Where>
     <OrderBy>
       <FieldRef Name='EventDate' Ascending='True' />
     </OrderBy>");
SPListItemCollection items = list.GetItems(query);

但这将 return 只有具有今天日期的项目。

试试这个,在 <Today/> 值中使用 OffsetDaysOffset 而不是 offset...

query.Query = string.Concat(@
    "<Where>
        <And>
          <Geq>
            <FieldRef Name='EventDate' />
            <Value IncludeTimeValue='False' Type='DateTime'><Today /></Value>
          </Geq>
          <Leq>
            <FieldRef Name='EventDate' />
            <Value IncludeTimeValue='False' Type='DateTime'><Today OffsetDays='30'/></Value>
          </Leq>
       </And>
     </Where>
     <OrderBy>
       <FieldRef Name='EventDate' Ascending='True' />
     </OrderBy>");

What is the difference between CAML Offset and OffsetDays?

或者您可以创建 DateTime 对象并使用 SPUtility.CreateISO8601DateTimeFromSystemDateTime method