Question

Again a calculated field question, How can I create a calculated field to find out, what is the age of the entry.

In the list I will have a created date field, from that I would like to create a number field calculating the no of days from created day to today. I tried with =Today-Created, not working!!

Any inputs ? Thanks !!

Was it helpful?

Solution

Unfortunately, calculated column approach will not work as expected :( Calculated fields are updated only if the column itself is being updated, or the item is updated. You can fiddle with setting date to future in Windows to prove it.

So I would recommend you to use XSL transformations for XsltListViewWebPart in conjunction with dateUtils.xsl from Marc D Anderson. I'm using this method for displaying "Birthday is soon" indicator in Contacts lists:

enter image description here

Works like a charm.

Tentative code example (this will display "age" of item in days):

      <xsl:variable name="DaysToday">
        <xsl:call-template name="countDaysInDateWithLeapYearDays">
          <xsl:with-param name="paramDate" select="substring-before(ddwrt:TodayIso(),'T')"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:variable name="DaysCreated">
        <xsl:call-template name="countDaysInDateWithLeapYearDays">
          <xsl:with-param name="paramDate" select="ddwrt:FormatDateTime(string($thisNode/@Created),1033,'YYYY-MM-dd')"/>
        </xsl:call-template>
      </xsl:variable>

      <div class="indicator">
        <xsl:value-of select="number($DaysToday)-number($DaysCreated)" />
      </div>

You should place this code into PrintTableCellEcbAllowed xsl template. And don't forget to include date_templates.xsl :) To learn more about XsltListViewWebPart templates, read following MSDN article:

OTHER TIPS

You can use Today with some trick, but the problem is that it will use the Today value from the day it was last modified. You'll then need to have something to modify the item each day/week/year, like a workflow.

http://weblogs.asp.net/bsimser/archive/2009/03/18/aging-calculated-fields-in-sharepoint.aspx

If its not used for any other logic, only for showing I would maybe used a javascript to calculate the difference, or use a CQWP with an itemstyle that does the calculations. Guess it depends what you'll be using the field for.

I have been successful with this formula: =DATEDIF([Created],[Today],"d") However, you have to create a Today column in the list leaving all the defaults. Then create this calculated column referencing that Today column. Then delete the Today column.

I wrote an article a while back about the different methods for creating a CountDown on a list view, which is essentially what you're looking for (well, a CountUp):

Warning: This also mentions our own product as a possible solution

How to create a Countdown in a SharePoint list

Here is a summary of the options:

  • JavaScript: Use a Content Editor Web Part.
  • Designer: Create a custom view in SharePoint Designer.
  • Code: Make your own custom field type from scratch.
  • SharePoint Highlighter: Buy our product.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top