My Query works when I'm querying text data from text column's whenever I start trying to Query a DateTime Field it falls down and consistently returns no results, the Regional Settings are set to Australia and im lead to believe it cant do the query because of time date formatting? can anyone elaborate further for me or better yet show me the code needed to get this query working.


             SPList myAnnouncements =                 new SPSite("http://mysite").OpenWeb().Lists["DateTime"];              string status = "Completed";              SPQuery myQuery = new SPQuery();             myQuery.Query = String.Format (                                  "<Where>" +                    "<Eq>" +                    "<FieldRef Name='Date1'/>" +                     "<Value IncludeTimeValue='FALSE' Type='DateTime'>[Today+2Month(s)]</value>" +                     "</Eq>" +                     "</Where>");              SPListItemCollection items = myAnnouncements.GetItems(myQuery);              foreach (SPListItem item in items)                 Console.Write(item["Title"].ToString());              Console.Read();                             
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 

Select allOpen in new window

asked 11/29/2011 10:53

kineticexpert's gravatar image

kineticexpert ♦♦

4 Answers:

I guess the value [Today+2Month(s)] is not correct (what is "s"). Try to calculate Date and put calculated Date in query instead of formula. The second thing to remember is that date should be in ISO 8601 format, so use SPUtility.CreateISO8601DateTimeFromSystemDateTime to format date value:

DateTime valDate = DateTime.Todays.AddMonths(2);
myQuery.Query = String.Format (
                "<Where>" +
                   "<Eq>" +
                   "<FieldRef Name='Date1'/>" +
                    "<Value IncludeTimeValue='FALSE' Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(valDate ) + "</value>" +
                    "</Eq>" +

For details on quering fields on different types see:


ivan_vagunin's gravatar image


Thanks for your input unfortunately the query provided doesn't work for me, not to sure why could it be anything to do with the fact of my Regional settings?

answered 2011-11-29 at 22:12:07

kineticexpert's gravatar image


Hi! TBW closing tag </value> is invalid, should be </Value> may be that is the problem? I'm not sure if it can because of regional settings.

answered 2011-11-30 at 15:49:10

ivan_vagunin's gravatar image


Although ivan_vagunin didn't completely fix my issue it was good help.


answered 2011-11-30 at 22:53:40

kineticexpert's gravatar image


Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments


Asked: 11/29/2011 10:53

Seen: 325 times

Last updated: 12/17/2011 05:21