Copied to clipboard

Flag this post as spam?

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


  • David Zweben 268 posts 754 karma points
    May 29, 2012 @ 22:08
    David Zweben
    0

    Get month name from 'MM' format string?

    I have a Press Releases section with a node tree in the following format:

    2012
    01
    Press Release 1
    03
    Press Release 2
    Press Release 3
    05
    2011
    03
    Press Release 4
    07
    Press Release 5

    I have an XSLT file that lists the month and Press Release nodes under each Year node, and I am looking for a way to display the full name of the month indicated in each month node's nodeName. For example, for 07, I would like to display "July". The createDate and updateDate of the Month nodes aren't necessarily going to match what I want to display; I want to go by the nodeName. What's the best way to convert the 'MM' format text into a full month name?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 29, 2012 @ 22:17
    Chriztian Steinmeier
    0

    Hi David,

    I've always just used a lookup variable for this, e.g.:

    <xsl:variable name="monthnameProxy">
        <month id="01">January</month> <month id="02">February</month> <month id="03">March</month>
        <month id="04">April</month> <month id="05">May</month> <month id="06">June</month>
        <month id="07">July</month> <month id="08">August</month> <month id="09">September</month>
        <month id="10">October</month> <month id="11">November</month> <month id="12">December</month>
    </xsl:variable>
    <xsl:variable name="monthnames" select="msxml:node-set($monthnameProxy)" />
    
    <xsl:template match="* | @*" mode="monthname">
        <xsl:value-of select="$monthnames[@id = current()]" />
    </xsl:template>
    

    With this in place, I can just use the apply-templates instruction to format any element containing a 01—12 value as a monthname, e.g.:

    <xsl:apply-templates select="$currentPage/@nodeName" mode="monthname" />

    /Chriztian 

  • David Zweben 268 posts 754 karma points
    May 29, 2012 @ 22:26
    David Zweben
    0

    Hi Chriztian,

    Thanks for the reply. That looks like a good way to do it, I'll have to try it out. I discovered my own way to do it as well, and it's a lot simpler, but it seems like it might be bad practice:

      <xsl:variable name="prMonthName">
        2000-<xsl:value-of select="@nodeName"/>-01T01:00:00
      xsl:variable>
      (<xsl:value-of select="umbraco.library:FormatDateTime($prMonthName, 'MMMM')"/>)

    Essentially, I am just adding in phony info to make the 'MM' string into a proper date string, and then using FormatDateTime to display only the month. It works, and it's very simple, but there might be a possibility of the phony info getting displayed somehow. Should I avoid doing this?

    I could make it a little safer to use by using the document's creation date as a starting point, then overriding the month part, but that hurts its simplicity.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 29, 2012 @ 22:36
    Chriztian Steinmeier
    0

    Hi David,

    Your way is another good way of doing it - but you should be very careful with the prMonthName variable though (whitespace etc.) - best way to create it would be like this:

    <xsl:variable name="prMonthName" select="concat('2000-', $currentPage/@nodeName, '-01T01:00:00')" />

    This way, you're not leaving anything up the processor, regarding which whitespace nodes are significant and should or should not be included. (Any extra whitespace would throw an exception when sent to the FormatDateTime() extension.)

    The reason I use the other method is that more often than not, the design (or the client) needs some of the monthnames abbreviated - e.g.: "May", "June" and "July" are fine, but the others should be "Jan.", "Feb." etc.

    One other benefit to yours, however, is that it will automatically output the monthnames in the chosen Culture for the node (e.g., if you're in a multilingual site) 

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft