Copied to clipboard

Flag this post as spam?

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


  • Duarte Carreira 47 posts 66 karma points
    Feb 06, 2011 @ 18:56
    Duarte Carreira
    0

    dates like 2007-07-01T00:00:00

    Hey there.

    In a xslt macro I'm listing an item properties and values. With dates I'm getting this format like:

    2007-07-01T00:00:00

    Why is this happening and how can I avoid it. I would like to not have to hardcode date fields in the macro.
    If a user inserts a date that looks like 2007-07-01 why would it be presented like 2007-07-01T00:00:00?

    Thanks,
    Duarte

  • Dan 1285 posts 3917 karma points c-trib
    Feb 06, 2011 @ 19:47
    Dan
    0

    Hi Duarte,

    That format is a standard date/time data type storage format.  It can be changed (reformatted) very easily in XSLT to give you full control over how the date/time is formatted.  There's an old wiki page on this which gives examples: http://en.wikibooks.org/wiki/Umbraco/Reference/umbraco.library/FormatDateTime.

    Hope this helps...

  • Chris Houston 535 posts 980 karma points MVP admin c-trib
    Feb 06, 2011 @ 20:42
    Chris Houston
    0

    Hi Duarte,

    It is also work having a look at the new Wiki and the EXSLT date functions.

    Cheers,

    Chris

  • Duarte Carreira 47 posts 66 karma points
    Feb 06, 2011 @ 21:21
    Duarte Carreira
    0

    Thanks for your assistance.

    How can we make the code in a more general fashion, so it works with any date field? Do we have to hardcode each field name that is a date? So every time we have a new date field we have to change our macro? This is what I'd like to achieve.

    Duarte

  • Chris Houston 535 posts 980 karma points MVP admin c-trib
    Feb 06, 2011 @ 21:23
    Chris Houston
    1

    Hi Duarte,

    What I have done in the past is actually write my own date format function that uses a setting stored in the web.config file, that way if the client wishes to change the date format used across their entire site, they can just change the mask in the web.config

    Cheers,

    Chris

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Feb 06, 2011 @ 22:02
    Chriztian Steinmeier
    1

    Hi Duarte,

    I've been doing this to keep consistent dates/times on pages: I have a template that matches any element or attribute in the "date" mode, which I can then use to format all the date values, whether they're standard Umbraco attributes (like createDate + updateDate) or DatePicker values:

    <xsl:template match="/">
        <p>
            I was born on: 
            <xsl:apply-templates select="$currentPage/dateOfBirth" mode="date" />
        </p>
    
        <p>
            Page updated on: 
            <xsl:apply-templates select="$currentPage/@updateDate" mode="date" />
        </p>
    </xsl:template>
    
    
    <!-- Template for any value that should be output as a date -->
    <xsl:template match="* | @*" mode="date">
        <time datetime="{.}">
            <xsl:value-of select="umbraco.library:FormatDateTime(., 'd MMM yyyy')" />
        </time>
    </xsl:template>
    
    (Usually I just use XSLT to format the date, but I guess most people know the strftime() equivalents for use with the library function...)

    /Chriztian 

  • Duarte Carreira 47 posts 66 karma points
    Feb 07, 2011 @ 01:23
    Duarte Carreira
    0

    Chriztian,

    That's brilliant, and soo simple!! I'll give it a try tomorrow.

    Thanks.

    Duarte

  • Duarte Carreira 47 posts 66 karma points
    Feb 08, 2011 @ 01:29
    Duarte Carreira
    0

    hmm, I can not apply this in a general way, I have to apply it on specified fields. I'm looking for a way to have a generic macro, that formats dates when they appear...

    so this template should only be applied to date fields, automatically. I'm trying out to test castable but I get errors from umbraco...

    The problem starts with how umbraco saves a date entered by the user, eg "01-01-2000", and not returning it the same way ("01-01-2000T000000").

    Duarte

  • Duarte Carreira 47 posts 66 karma points
    Feb 08, 2011 @ 18:13
    Duarte Carreira
    0

    ok, used a modified version of Chriztian's template approach:

    <xsl:template match="* | @*">
      <xsl:choose>
        <xsl:when test="umbraco.library:FormatDateTime(., 'd MMM yyyy') = ''">
           <xsl:value-of select="."/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="umbraco.library:FormatDateTime(., 'd/MM/yyyy')" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    

    Duarte

Please Sign in or register to post replies

Write your reply to:

Draft