Filtering views by Time and Date
An earlier blog entry describes how to filter List and Document views by date. However, the [Today] function, by default, returns only the date and not the time. Therefore, this solution does not solve mad-tie’s problem of wanting to filter on items created in the last hour.
CAML queries can include time as well as date when using the [Today] function. This is described by this blog by ucsharp. In CAML when using the [Today] function you can use the “IncludeTimeValue” attribute to specify that time as well as date should be used:
<Value Type=”DateTime” IncludeTimeValue=”TRUE”><Today /></Value>
Now, the problem is that you cannot include this attribute through the SharePoint user interface, but it can be done with SharePoint Designer. So, to create a view that displays items created in the last hour:
- Create a calculated column called “DatePlusHour” of “Date and Time” type using the “Date & Time” format in the list or document library (as done by mad-tie):
This takes the date/time of when the item was created and adds one hour (1/24 = 0.4166…).
- Modify the view (e.g. “All Items”) to filter based on [Today]:
Now the CAML query so created needs to be manually edited to include the IncludeTimeValue attribute:
- Run Microsoft Designer and open the site in which the list or document library resides.
- Locate the list or document library and open the .aspx file associated with the view (e.g. “AllItems.aspx”.
- Click the Code tab and locate the CAML query associated with the filter (search on “DatePlusHour”).
- Edit the CAML query to include the IncludeTimeValue attribute. NOTE: Include spaces exactly as show!
<Value Type="DateTime" IncludeTimeValue="True">
It should look like the following:
WARNING: If the SharePoint user interface is used to modify the view the IncludeTimeValue attribute will need to be readded using the instructions above.