Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alexandru 112 posts 272 karma points
    Oct 03, 2013 @ 12:25
    Alexandru
    0

    Trouble getting PostDate in XSLT

    Hi, I am trying to make a macro that calculates how long ago a blog post has been posted.

    The problem is when I try to get the current blogpost date.

    Here is the code:

    <xsl:variable name="currentDate" select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), 'dd-MM-yyyy')"/>
    <xsl:variable name="postDate" select="umbraco.library:FormatDateTime(currentPage/@PostDate, 'dd-MM-yyyy')"/>
    <xsl:variable name="diffSeconds" select="umbraco.library:DateDiff($currentDate, $postDate, 's')"/>
    <xsl:variable name="diffDays" select="floor($diffSeconds div (60 * 60 * 24))"/>
    

    The error is placed at currentPage/@PostDate and it says:

    System.FormatException: String was not recognized as a valid DateTime.

    Isn't it supposed to run well since I have mentioned the context?

  • Kim Nedergaard 37 posts 144 karma points
    Oct 03, 2013 @ 12:29
    Kim Nedergaard
    0

    You're missing "$" infront of your "currentPage".

    Should be:

    <xsl:variable name="postDate" select="umbraco.library:FormatDateTime($currentPage/@PostDate, 'dd-MM-yyyy')"/>
    

    Could that be the error?

  • Alexandru 112 posts 272 karma points
    Oct 03, 2013 @ 12:30
    Alexandru
    0

    I tried that aswell, same error.

  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 12:32
    Rich Green
    0

    Assuming postDate is a DocType property:

    <xsl:variable name="postDate" select="umbraco.library:FormatDateTime($currentPage/postDate, 'dd-MM-yyyy')"/>
  • Alexandru 112 posts 272 karma points
    Oct 03, 2013 @ 12:34
    Alexandru
    0

    The DocType property is PostDate. Still, I get the same thing.... even without using "@"

  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 12:37
    Rich Green
    0

    make sure you can get the property out first, one step at a time, what does this return?

    <xsl:value-of select="$currentPage/postDate"/>
  • Kim Nedergaard 37 posts 144 karma points
    Oct 03, 2013 @ 12:41
    Kim Nedergaard
    0

    Hi again,

    Maybe you need to wrap an if around, not all pages have the @postDate attribute.

    <xsl:if test="$currentPage/@PostDate != ''">
    <xsl:variable name="currentDate" select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), 'dd-MM-yyyy')"/>
    <xsl:variable name="postDate" select="umbraco.library:FormatDateTime($currentPage/@PostDate, 'dd-MM-yyyy')"/>
    <xsl:variable name="diffSeconds" select="umbraco.library:DateDiff($currentDate, $postDate, 's')"/>
    <xsl:variable name="diffDays" select="floor($diffSeconds div (60 * 60 * 24))"/>
    </xsl:if>
    

    The "FormatDateTime", doesn't like to be called without a date :)

  • Alexandru 112 posts 272 karma points
    Oct 03, 2013 @ 13:17
    Alexandru
    0

    @Rich Green

    That line of code returns empty it seems. Which means It can't get the property out which is really weird because outside this macro, in the HTML I use this to get the postDate

    <span><umbraco:Item runat="server" field="PostDate" xslt="umbraco.library:FormatDateTime({0},'dd, MMM yyyy')"/></span>
    

    @Kim Nedergaard

    It doesn't help. It only makes the error go away by skipping those few lines of code.

    This is what the macro looks like entirely:

    <xsl:output method="html" omit-xml-declaration="yes"/>
    
    <xsl:param name="currentPage"/>
    
    <xsl:template match="/">
    
                <span id="timer">
                    <xsl:variable name="currentDate" select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), 'dd-MM-yyyy')"/>
                    <xsl:variable name="postDate" select="umbraco.library:FormatDateTime($currentPage/PostDate, 'dd-MM-yyyy')"/>
                    <xsl:variable name="diffSeconds" select="umbraco.library:DateDiff($currentDate, $postDate, 's')"/>
                    <xsl:variable name="diffDays" select="floor($diffSeconds div (60 * 60 * 24))"/>
                    <xsl:choose>
                        <xsl:when test="$diffDays = 0">
                            I dag
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$diffDays"/> dage siden
                        </xsl:otherwise>
                    </xsl:choose>
    
    
                </span>
    
    </xsl:template>
    
  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 13:21
    Rich Green
    0

    Can you post a snippet from your /App_Data/umbraco.config file of a line that contains postDate

  • Alexandru 112 posts 272 karma points
    Oct 03, 2013 @ 13:23
    Alexandru
    0
                <PostDate>2013-10-01T18:46:00</PostDate>
    

    This one?

  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 13:25
    Rich Green
    0

    yes, you need to be using

    <xsl:value-ofselect="$currentPage/PostDate"/>

  • Alexandru 112 posts 272 karma points
    Oct 03, 2013 @ 13:31
    Alexandru
    0

    This is what I need:

    <xsl:variable name="postDate" select="umbraco.library:FormatDateTime($currentPage/PostDate, 'dd-MM-yyyy')"/>
    

    If I try this:

    <xsl:value-ofselect="$currentPage/PostDate"/>
    

    it returns the right date and time but I need to format it and the above line of code won't work. If you look at the whole macro I have posted - 'postDate' is a variable and 'PostDate' is the property from the DocType.

  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 13:33
    Rich Green
    0

    As Kim's solution one the previous page:

    <xsl:iftest="$currentPage/PostDate != ''">
    <xsl:variablename="currentDate"select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), 'dd-MM-yyyy')"/>
    <xsl:variablename="postDate"select="umbraco.library:FormatDateTime($currentPage/PostDate, 'dd-MM-yyyy')"/>
    <xsl:variablename="diffSeconds"select="umbraco.library:DateDiff($currentDate, $postDate, 's')"/>
    <xsl:variablename="diffDays"select="floor($diffSeconds div (60 * 60 * 24))"/>
    </xsl:if>
  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 13:35
    Rich Green
    100

    The editor messed up the code:

     

    <xsl:if test="$currentPage/PostDate != ''">
        <xsl:variable name="currentDate"select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), 'dd-MM-yyyy')"/>
        <xsl:variable name="postDate"select="umbraco.library:FormatDateTime($currentPage/PostDate, 'dd-MM-yyyy')"/>
        <xsl:variable name="diffSeconds"select="umbraco.library:DateDiff($currentDate, $postDate, 's')"/>
        <xsl:variable name="diffDays"select="floor($diffSeconds div (60 * 60 * 24))"/>
    </xsl:if>
  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 13:35
    Rich Green
    1

    (note the corrected $currentPage/PostDate)

     

  • Alexandru 112 posts 272 karma points
    Oct 03, 2013 @ 13:39
    Alexandru
    0
    <xsl:if test="$currentPage/PostDate != ''">
                        <xsl:variable name="currentDate" select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), 'dd-MM-yyyy')"/>
                        <xsl:variable name="postDate" select="umbraco.library:FormatDateTime($currentPage/PostDate, 'dd-MM-yyyy')"/>
                        <xsl:variable name="diffSeconds" select="umbraco.library:DateDiff($currentDate, $postDate, 's')"/>
                        <xsl:variable name="diffDays" select="floor($diffSeconds div (60 * 60 * 24))"/>
                    </xsl:if>
                    <xsl:choose>
                        <xsl:when test="$diffDays = 0">
                            I dag
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$diffDays"/> dage siden
                        </xsl:otherwise>
                    </xsl:choose>
    

    Error strikes at this line now:

                            <xsl:when test="$diffDays = 0">
    
  • Alexandru 112 posts 272 karma points
    Oct 03, 2013 @ 13:41
    Alexandru
    0

    Oups, fixed it. I surrounded the whole choose with the IF statement.

    Seems to be working now!

    Thanks you very much!

  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 13:43
    Rich Green
    0

    No problem, glad you got it sorted, remember to mark the solution as solved as it helps others too :)

  • Alexandru 112 posts 272 karma points
    Oct 03, 2013 @ 13:44
    Alexandru
    0

    Should I post the solution myself? The whole macro, just so others can use it aswell?

  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 13:45
    Rich Green
    0

    No, sorry, there's a little tick where you can mark the solution as solved (next to the post that solved the issue)

Please Sign in or register to post replies

Write your reply to:

Draft