SharePoint list view filter by hours

The [Today] parameter is a  dynamic parameter that can be add some enormous benefit in creating a custom view.  Using {Today] is powerful because it can filter a column based on the current date.

Some examples of ways to use the [Today] parameter are:

  • Displaying items created over the past week
    • with a filter for showing “Created is greater than or equal to  [Today]-7”
    • This view could be called “Recent Stuff”
  • Displaying items or documents that have expired:
    • with a filter for showing “Expiration Date is less than [Today]”
  • Displaying deadlines values that are less than 7 days from now:
    • with a filter for showing “Deadline is greater than [Today]+7”
  • Showing all tasks that are due in the next 7 days or overdue:
    • with a filter for showing “Due Date is less than [Today]+7 OR Status is not equal to Completed”

Steps to Create a [Today] filtered view:

  1. Go to your Library or List Settings.
  2. Click on Create a View.
  3. In the view creation screen, scroll down to the Filter section.
    • Filter for the desired situation.  Use the examples above as a guide to set the parameters as needed.
    • In this screenshot, I am filtering for all documents that have been created OR modified in the last 3 days.  These settings result in creating a “Recent Stuff” view.
    • When typing the expression using the  [Today] parameter, make sure to type the entire expression without any spaces.

One limitation of the [Today] field is that it cannot be used for formulas in Calculated Columns.

Related Posts:

How to Create a Dynamic [Me] Filtered View in SharePoint

SharePoint Resources

Please leave a comment if you found this post useful.

There are a huge number of articles out there discussing how to create [semi-] dynamic list views or filters based on some volatile property like current time. For instance: “Show me all documents that were created today” or “Show me all documents that were created more than 7 days ago” or “Show me all in tasks due in the next three days”. The solution to this is some combination of a list view filter, or calculated column, and the use of a property like [Today]. [Note: There are a couple of key caveats to do with this.]

A limitation of this, however, is that the time portion of a datetime is ignored. So when I wanted to “show all documents created in the last 10 minutes” - I couldn’t. There were a few workarounds - various combinations of calculated columns, using SharePoint Designer and so on were available - but I was determined to find a solution that didn’t require any of this!

Alas, I failed. I tried lots of things - Content Query web part, Data View Web Part, calculated columns, custom CAML - but whilst some of it worked, there was either always way too much plumbing required to get it in and it wasn’t particularly deployable between our Dev, Test and Prod environments.

The “least pain” and most deployable solution I arrived at is as follows. This describes what I did to achieve my specific requirements - you will need to adjust the parameters and logic to suit your needs. So I wanted a list view that showed all documents in a library that were created more than 10 minutes ago.

  1. Unfortunately* - create a calculated column on the field that contains the datetime you want to compare against, such as [Created] or [Modified], and specify the formula to include the time offset you need. So in my case, I wanted [Created] + 10 minutes, which looks like**

[Created] + 0.007

Call your column something memorable - e.g., CalculatedPlus10. You can verify this is doing what you want by adding it to a view and comparing its value against your reference field.

  1. Now create a new view. It’s easiest if you start from an existing view, e.g., All Documents. Call it something like “CreatedPlus10”

  2. Using Powershell***, I then set the Query for my new view, to use the new column. The trick here is that via the UI, you cannot tell a View to respect the time portion of the datetime field - that’s essentially what this query is doing:

Note that the ” are escaped. This compares the value of your calculated column, and finds items that are Less Than now, including the time portion.

Then update the View and as if by magic, your view will now only show items that were created more than 10 minutes ago.

Your completed Powershell script might look a bit like this:

$web = get-spweb //yourserver $list = $web.Lists[“YourListName”] $view = $list.Views[“CreatedPlus10”]

$view.Query = “”

$view.Update[]

$web.Dispose[]

I previously shared how to create a “Today” column in SharePoint that would always be up-to-date even if list items weren’t modified. These were no-code solutions that utilized either SharePoint Designer or Microsoft Flow. You can, however, use Today’s date/time to create views and calculated columns without workflow or script or the need to create another column.

“Today” in list and library views

In views, you’ll need to set filters using

[Today]

You could also create more complicated filters such as:

  • Incomplete projects: DueDate = [[Today]+7]
  • Last year on this day: Created = [Today]-365

Calculated columns with today and/or current time

In calculated columns, you’ll need to use

Today[]

or

Now[]

as the value for today’s date or today’s date and time.

The difference is Now[] uses date and time, so you’ll get down-to-the-minute values [i.e. hours since reported]. Today is just date best used for data with no times involved [i.e. days since hire date]. The following is a side-by-side comparison using the same “Created” date subtracted from Today[] and Now[]. You’ll notice the value using Now[] is a little higher because it includes the hours already passed today whereas Today[] hasn’t changed since midnight.

Note: Views using “Today” will always show correctly. The calculated column methods above, however, are a snapshot of today or the current time. They will not update automatically or regularly; just when a list item is modified.

If you need a “live” always-updated value regardless of items being modified, you’ll need to create a separate “Today” column using Microsoft Flow or SharePoint Designer:

This post also includes ideas for calculations using today’s date.

Video liên quan

Chủ Đề