Copied to clipboard

Flag this post as spam?

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


  • Connie DeCinko 931 posts 1159 karma points
    May 02, 2012 @ 17:38
    Connie DeCinko
    0

    Format longer dates with abbreviation

    I stared off using this to format an abreviated month for the date:

    <umbraco:Item field="publicationDate" xslt="umbraco.library:FormatDateTime({0},'MMM. d, yyyy')" stripParagraph="true" runat="server"></umbraco:Item>

    Problem is, I don't want the period for May and I'd prefer to show the full month for June and July. Any easy way to do this?

     

  • Tom Fulton 2030 posts 4996 karma points c-trib
    May 02, 2012 @ 18:47
    Tom Fulton
    1

    Hi Connie,

    I don't think there's a simple way, short of creating a new XSLT/Razor file and testing for your conditions and using different formats in each situation.

    Maybe something like this (not tested):

     

    <xsl:variable name="yourDate" select="$currentPage/publicationDate" />
    <xsl:variable name="monthName" select="umbraco.library:FormatDateTime($yourDate, 'MMMM')" />
    <xsl:variable name="dateFormat">
      <xsl:choose>
        <xsl:when test="$monthName = 'May'">MMM d, yyyy</xsl:when>
        <xsl:when test="$monthName = 'June' or $monthName = 'July'">MMMM d, yyyy</xsl:when>
        <xsl:otherwise>MMM. d, yyyy</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:value-of select="umbraco.library:FormatDateTime($yourDate, $dateFormat)" />

    HTH,
    Tom 

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    May 03, 2012 @ 08:17
    Chriztian Steinmeier
    0

    Hi Connie,

    Whenever I've had anything that resembled special formatting that doesn't directly translate to the built-in formatting functions, I've done something like this to make sure that I'd get the same format *every* time it was needed:

    <!-- Lookup variable with pre-formatted monthnames -->
    <xsl:variable name="monthNamesProxy">
        <month>Jan.</month> <month>Feb.</month> <month>Mar.</month>
        <month>Apr.</month> <month>May</month> <month>June</month>
        <month>July</month> <month>Aug.</month> <month>Sep.</month>
        <month>Nov.</month> <month>Oct.</month> <month>Dec.</month>
    </xsl:variable>
    <xsl:variable name="months" select="msxml:node-set($monthNamesProxy)/month" />
    
    <xsl:template match="/">
    
        <!-- Format the publicationDate property as a date -->
        <xsl:apply-templates select="$currentPage/publicationDate" mode="date" />
    
    <!-- Format the @createDate property (attribute) as a date -->
    <xsl:apply-templates select="$currentPage/@createDate" mode="date" />
    
    </xsl:template>
    
    <!-- Template for formatting dates -->
    <xsl:template match="* | @*" mode="date">
        <xsl:variable name="month" select="number(substring(., 6, 2))" />
        <xsl:variable name="date" select="number(substring(., 9, 2))" />
        <xsl:variable name="year" select="substring(., 1, 4)" />
    
        <xsl:value-of select="concat($months[$month], ' ', $date, ', ', $year)" />
    </xsl:template>
    

    /Chriztian 

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies