Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 !!

share|improve this question
    
A similar question: stackoverflow.com/questions/878744/… –  Andrey Markeev Nov 2 '11 at 12:14
add comment

4 Answers 4

up vote 5 down vote accepted

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:

share|improve this answer
    
Thanks, omlin let me see how to get this worked in a list column, hopefully it will for each list item separately :) –  Jithu Nov 2 '11 at 12:03
    
Of course it will. –  Andrey Markeev Nov 2 '11 at 13:16
    
Added XSLT code sample to the answer –  Andrey Markeev Nov 2 '11 at 13:24
    
Thanks omlin!! This will really help me!! –  Jithu Nov 2 '11 at 13:26
add comment

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.

share|improve this answer
    
Hi Andez, Thanks for helping me again, I am just using this field only for our reference purpose, like we have a task generated and we need to know , how old that task is? And we have to display it in the list!! –  Jithu Nov 2 '11 at 10:58
    
How can we use Javascript to calculate a column values, I really don't know that method!! –  Jithu Nov 2 '11 at 11:11
    
Hi as you said, I just went through the link you provided, however the problem you stated is really a mess. However got a good link for updating the fields dynamically - here it is . Looks like its gonna be a long day for just getting some days calculated !! Thanks !! –  Jithu Nov 2 '11 at 11:22
add comment

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.

share|improve this answer
    
Thank you spevil, I think still I should create a separate TODAY column!! –  Jithu Nov 2 '11 at 11:25
    
Getting Error!! =DATEDIF([Created],[Today],"d") –  Jithu Nov 2 '11 at 11:29
    
I corrected the entry. I should have better explained it! –  spevilgenius Nov 2 '11 at 11:43
1  
I'm afraid this will not work :( Based on my own experience. Also have a look to this post: blog.pentalogic.net/2008/11/… –  Andrey Markeev Nov 2 '11 at 11:56
1  
My solution works for what I used it for. However, I like the XSL approach and Marc's stylesheet is very useful. –  spevilgenius Nov 2 '11 at 14:44
show 3 more comments

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.