user302593
user302593

Reputation: 121

CAML filter to get the current month from the list?

In SharePoint 2010, I need to get items from a list based on a condition. Considering one of the fields to be 'Date' of type DateTime, the condition is:

Get Current Month Data.

How do I filter the list items based on this condition using CAML query?

By, Raji

Upvotes: 1

Views: 4442

Answers (1)

Dmitrii Dovgopolyi
Dmitrii Dovgopolyi

Reputation: 6301

Use SPUtility.CreateISO8601DateTimeFromSystemDateTime to create relevant dateTime string

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);

Upvotes: 3

Related Questions