CAML过滤器从列表中获取当前月份?

时间:2012-10-10 08:59:56

标签: sharepoint caml

在SharePoint 2010中,我需要根据条件从列表中获取项目。考虑其中一个字段是DateTime类型的'Date',条件是:

获取当月数据。

如何使用CAML查询根据此条件过滤列表项?

通过, 的Raji

1 个答案:

答案 0 :(得分:3)

使用SPUtility.CreateISO8601DateTimeFromSystemDateTime创建相关的dateTime字符串

DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
Sting stringQuery =
String.Format(@"<And>
                  <Geq>
                   <FieldRef Name='Date' />
                   <Value Type='DateTime'>{0}</Value>
                 </Geq>
                 <Leq>
                  <FieldRef Name='Date' />
                  <Value Type='DateTime'>{1}</Value>
                 </Leq>
                </And>", 
                SPUtility.CreateISO8601DateTimeFromSystemDateTime(firstDay),
                SPUtility.CreateISO8601DateTimeFromSystemDateTime(firstDay .AddMonths(1)));
SPQuery query = new SPQuery(stringQuery);