Copied to clipboard

Flag this post as spam?

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


  • Laurence Gillian 600 posts 1219 karma points
    Mar 16, 2010 @ 16:08
    Laurence Gillian
    2

    Week Start and End Dates

    On a project I've been working on, we need to grab the week start date and end date (assuming it starts on Monday).

    I had a look around on the internet and found some very good code, but it was a bit epic at over 150 lines! 

    So I've quickly shrunk it down using some Umbraco helper methods. Just posting it up here as it may help someone use and save them a few hours.

    Any further shrinks are more than welcomed! ;) Lau

    <xsl:variable name="dateCurrent" select="Exslt.ExsltDatesAndTimes:date()" />
        <xsl:variable name="dateName" select="umbraco.library:FormatDateTime($dateCurrent, 'dddd')" />
        <xsl:variable name="dateSubtract">
            <xsl:choose>
                <xsl:when test="$dateName = 'Monday'">0</xsl:when>
                <xsl:when test="$dateName = 'Tuesday'">1</xsl:when>
                <xsl:when test="$dateName = 'Wednesday'">2</xsl:when>
                <xsl:when test="$dateName = 'Thursday'">3</xsl:when>
                <xsl:when test="$dateName = 'Friday'">4</xsl:when>
                <xsl:when test="$dateName = 'Saturday'">5</xsl:when>
                <xsl:when test="$dateName = 'Sunday'">6</xsl:when>
                <xsl:otherwise>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="dateWeekStarts" select="umbraco.library:DateAdd(umbraco.library:CurrentDate(), 'd', -$dateSubtract)" />
        <xsl:variable name="dateWeekEnds" select="umbraco.library:DateAdd($dateWeekStarts, 'd', '7')" />
        <xsl:value-of select="$dateWeekStarts"/>
        <xsl:value-of select="$dateWeekEnds" />
  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Mar 16, 2010 @ 22:04
    Chriztian Steinmeier
    1

    Hi Laurence,

    Thought I'd give it a go, just to see what I'd need to change for my local non-Umbraco environments (where I don't have access to the umbraco.library, only the exslt.org extensions) - so here's a Exslt-only version (there's an exslt-equivalent for msxml:node-set() which I couldn't find in the Umbraco version of Exslt) - enough already, here's the stuff:

           <xsl:variable name="days">
                <day weekday="2" name="Monday" sub="0" />
                <day weekday="3" name="Tuesday" sub="1" />
                <day weekday="4" name="Wednesday" sub="2" />
                <day weekday="5" name="Thursday" sub="3" />
                <day weekday="6" name="Friday" sub="4" />
                <day weekday="7" name="Saturday" sub="5" />
                <day weekday="1" name="Sunday" sub="6" />
            </xsl:variable>
    
            <xsl:variable name="today" select="date:date()" />
            <xsl:variable name="weekday" select="date:dayinweek($today)" />
            <xsl:variable name="dayDiff" select="msxml:node-set($days)/day[@weekday = $weekday]/@sub" />
    
            <xsl:variable name="weekStartsOn" select="date:add($today, concat('-P', $dayDiff, 'D'))" />
            <xsl:variable name="weekEndsOn" select="date:add($weekStartsOn, 'P7D')" />
    
            <!-- Output values -->
            <p>Week starts on: <xsl:value-of select="$weekStartsOn"/></p>
            <p>Week ends on: <xsl:value-of select="$weekEndsOn" /></p>        
    

    (So the "date" prefix is mapped to the URI "urn:Exslt.ExsltDatesAndTimes") - the weird 'P7D' stuff is the "duration" argument to the add() function, which is described here: http://www.w3.org/TR/xmlschema-2/#duration

    Also, I use an XML variable to map the value from weekday() to the number of days needed to subtract from today (the name attributes in this variable is entirely for the humans reading it...)

    Cheers,

    Chriztian

  • Giorgos Grispos 145 posts 179 karma points
    Feb 28, 2011 @ 10:32
    Giorgos Grispos
    0

    Hello Chriztian,

    I tried to use your piece of code with no luck, actually the preffix date is not defined.

    Any ideas?

    Cheers, Giorgos

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Feb 28, 2011 @ 10:57
    Chriztian Steinmeier
    0

    Hi Giorgios,

    No - that part was left out (but I explained in below the code).

    Ususlly, the prefixes are mapped to a namespace URI on the root element of an XML file, in this case the xsl:stylesheet element:

    <xsl:stylesheet version="1.0"
        xmlns:date="urn:Exslt.ExsltDatesAndTimes"
        ...
        exclude-result-prefixes="date ..."
    >

    The dots (...) refers to additional definition/exclusions that may already be there.

    /Chriztian

  • Giorgos Grispos 145 posts 179 karma points
    Feb 28, 2011 @ 11:46
    Giorgos Grispos
    0

    Oh yes,

    I thought it was already there! Now it's fine, thanks a lot.

    Giorgos

Please Sign in or register to post replies

Write your reply to:

Draft