Skip to main content

Cloud

Calculating the Age of a Document in SharePoint

Getting the age of a document in days can be a bit of a pain. If you use a calculated column function like = Today – Created the calculation will only be run when the document is added and is not recalculated when the document

library is displayed. To address this we need to add a date calculation in the XSLT data view that will compare the documents Created date against the current time. This can be done using FrontPage.

There are a few things that need to be done before we start editing the view though: First add a new field to the document library. Call it Age or something like that. You can chose any data type but number is right-aligned by default which is what you’ll probably want.

Next, create a new view that includes the Age column or add it to an existing view. Be sure your view also includes the data column you want to use as the basis for calculating the age. In this example I’ll use the Created column. I’d suggest setting all of your sorting, filtering, and grouping options ahead of time because you won’t be able to make the changes using the SharePoint interface later.

Next, enter some dummy data into the Age field; to make editing in FrontPage easier.

Now, open the View in FrontPage and switch to split view. (Be sure to make a back up first, this is easy to mess up.)

  1. Click on the document list then right-click and select "Convert to XSLT Data View"
  2. Select one of the values in your custom Age column
  3. Just above the page in the tag selector you should see <xsl-value-of> highlighted.
  4. Click on it and select "Edit Tag"

[ImageAttachment]

  1. You should see something like "<xsl:value-of disable-output-escaping=’no’ select=’format-number(string(@Age), ‘#,##0.###;-#’)’/>"
  2. Copy this tag and find it in the code editor.
  3. Comment out the tag and insert the following code below it:

    <!– DATE CALC STARTS HERE –>
    <!– Get the current Month and convert to a number–> <xsl:variable name="nowMonth" select="number(substring(ddwrt:ThreadStamp(),5,2))"/>
    <!– Get the current Year and convert to a number–> <xsl:variable name="nowYear" select="number(substring(ddwrt:ThreadStamp(),1,4))"/>
    <!– Get the current Day and convert to a number–> <xsl:variable name="nowDay" select="number(substring(ddwrt:ThreadStamp(),7,2))"/>
    <!– Add the appropriate number of days in the year based on the current selected month
    example: Jan: add 0 days, Feb: add 31 days from Jan –> <xsl:variable name="nowAddDays">
    <xsl:choose xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:when test="$nowMonth=1"><xsl:value-of select="number(0)"/></xsl:when>
    <xsl:when test="$nowMonth=2"><xsl:value-of select="number(31)"/></xsl:when>
    <xsl:when test="$nowMonth=3"><xsl:value-of select="number(59)"/></xsl:when>
    <xsl:when test="$nowMonth=4"><xsl:value-of select="number(90)"/></xsl:when>
    <xsl:when test="$nowMonth=5"><xsl:value-of select="number(120)"/></xsl:when>
    <xsl:when test="$nowMonth=6"><xsl:value-of select="number(151)"/></xsl:when>
    <xsl:when test="$nowMonth=7"><xsl:value-of select="number(181)"/></xsl:when>
    <xsl:when test="$nowMonth=8"><xsl:value-of select="number(212)"/></xsl:when>
    <xsl:when test="$nowMonth=9"><xsl:value-of select="number(243)"/></xsl:when>
    <xsl:when test="$nowMonth=10"><xsl:value-of select="number(273)"/></xsl:when>
    <xsl:when test="$nowMonth=11"><xsl:value-of select="number(304)"/></xsl:when>
    <xsl:when test="$nowMonth=12"><xsl:value-of select="number(334)"/></xsl:when>
    <xsl:otherwise><xsl:value-of select="number(0)"/></xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    <!– Get values for the document created date and convert to numbers–> <xsl:variable name="crYear" select="number(substring(@Created_x0020_Date,1,4))"/>
    <xsl:variable name="crMonth" select="number(substring(@Created_x0020_Date,6,2))"/>
    <xsl:variable name="crDay" select="number(substring(@Created_x0020_Date,9,2))"/>
    <xsl:variable name="crAddDays">
    <xsl:choose xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:when test="$crMonth=1"><xsl:value-of select="number(0)"/></xsl:when>
    <xsl:when test="$crMonth=2"><xsl:value-of select="number(31)"/></xsl:when>
    <xsl:when test="$crMonth=3"><xsl:value-of select="number(59)"/></xsl:when>
    <xsl:when test="$crMonth=4"><xsl:value-of select="number(90)"/></xsl:when>
    <xsl:when test="$crMonth=5"><xsl:value-of select="number(120)"/></xsl:when>
    <xsl:when test="$crMonth=6"><xsl:value-of select="number(151)"/></xsl:when>
    <xsl:when test="$crMonth=7"><xsl:value-of select="number(181)"/></xsl:when>
    <xsl:when test="$crMonth=8"><xsl:value-of select="number(212)"/></xsl:when>
    <xsl:when test="$crMonth=9"><xsl:value-of select="number(243)"/></xsl:when>
    <xsl:when test="$crMonth=10"><xsl:value-of select="number(273)"/></xsl:when>
    <xsl:when test="$crMonth=11"><xsl:value-of select="number(304)"/></xsl:when>
    <xsl:when test="$crMonth=12"><xsl:value-of select="number(334)"/></xsl:when>
    <xsl:otherwise><xsl:value-of select="number(0)"/></xsl:otherwise>
    </xsl:choose></xsl:variable>
    <!– subtract the document year from the current year and multiple by 356 days subtract the document month days plus day from the current month days plus day value –>
    <xsl:value-of disable-output-escaping="no" select="(($nowYear – $crYear) * 365) + ($nowAddDays + $nowDay) – ($crAddDays + $crDay)"/>
    <!– DATE CALC ENDS HERE –>

  4. Save your page.

You may need to replace the @Created_x0020_Date variable with the correct value for your view. It will be slightly different in a List View for instance.

Note: This is a rough calculation only and does not account for leap years or hours and minutes, but those could be added following the same method as above.

Technical Notes: The ThreadStamp() function from the ddwrt namespace function returns the current time (Now) as a string in the format: yyyyMMddHHmmss

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

PointBridge Blogs

More from this Author

Follow Us