Copied to clipboard

Flag this post as spam?

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


  • karen 186 posts 461 karma points
    Aug 09, 2011 @ 19:43
    karen
    0

    building a date and then using date add

    I am passing in a month to my macro as a parameter and gettting it like this:

    <xsl:param name="defaultStartMonth" select="/macro/defaultStartMonth"/>

    I then want to build a date string using that, so for example of the defaultStartMonth was 9, the date string would be 9/1/2011 (1st day of month, current year)

    So I am doing this (I have tried a number of different things)

    <xsl:variable name="startDateShow">
    $defaultStartMonth/
    <xsl:value-of select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), '1/yyyy')"/>
    </
    xsl:variable>

    This will let me save the xslt file.

    Next I want to get an end date by adding 1 year, so trying this:

    <xsl:variable name="endtDateShow">
    <xsl:value-of select="umbraco.library:DateAdd($startDateShow, 'y', 1)"/>
    </xsl:variable>

    but this won't let me save, I get an error (doesn't give a line number)

    Error occured
    System.FormatException: String was not recognized as a valid DateTime.
    at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
    at umbraco.library.DateAdd(String Date, String AddType, Int32 add)

    The error is occuring on the DateAdd because if I take that line out, the error goes away.

    The following does work

    <xsl:variable name="startMonthShow">
    <xsl:value-of select="umbraco.library:FormatDateTime($startDateShow, 'MM')"/>
    </xsl:variable>

    so in that case $startDateShow can be formatted as a date time, I just can't add a year to it.  I have even tried

    <xsl:variable name="endtDateShow">
    <xsl:value-of select="umbraco.library:DateAdd(umbraco.library:FormatDateTime($startDateShow, 'MM/dd/yyyy'), 'y', 1)"/>
    </xsl:variable>

    but get the same error.

    Any suggestions?

  • karen 186 posts 461 karma points
    Aug 09, 2011 @ 19:49
    karen
    0

    One more note, if I use current date instead of trying to build a date, everything works fine:

    <xsl:variable name="startDateShow">
    <xsl:value-of select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), 'MM/1/yyyy')"/>
    </xsl:variable>
  • Dirk Seefeld 126 posts 665 karma points
    Aug 09, 2011 @ 21:27
    Dirk Seefeld
    0

    Hi karen,

    try this:

    <xsl:variable name="defaultStartMonth" select="9"/>
      
      <xsl:variable name="startDateShow" select="concat($defaultStartMonth,'/',umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), '1/yyyy'))"/>
      <xsl:variable name="endtDateShow" select="concat($defaultStartMonth,'/',umbraco.library:FormatDateTime(umbraco.library:DateAdd(umbraco.library:CurrentDate(),
    'y', 1), '1/yyyy'))"
    />
      
      <xsl:value-of select="$startDateShow"/><br />
      <xsl:value-of select="$endtDateShow"/>

    Greetings Dirk

  • karen 186 posts 461 karma points
    Aug 09, 2011 @ 23:07
    karen
    0

    Hi,

    Thanks Dirk.

    It is more complicated then that though, I can't base endDateShow off the defaultStartMonth because that is only default if nothing is passed in on the query string.  Also, startDateShow is based on that also. I only included a simple version of the code.  But basically startDateShow is figured out, and then endDateShow is calculated from that.  Here is the full code for figuring startDateShow, changed to use your suggestion above.

    <xsl:variable name="startDateShow">
    <xsl:choose>
    <xsl:when test="umbraco.library:RequestQueryString('startDate') = ''">
        <xsl:value-of select="concat($defaultStartMonth,'/',umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), '1/yyyy'))"/>
    "/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="umbraco.library:FormatDateTime(umbraco.library:RequestQueryString('startDate'), 'MM/dd/yyyy')"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

    but this stll gives me the same error when trying to do endDateShow.

    It seems that doing the DateAdd on a constructed date string is not working.

  • karen 186 posts 461 karma points
    Aug 09, 2011 @ 23:27
    karen
    0

    Actually, doing any Date function (like DateGreaterThanOrEqual) on a 'built' string is not working.

    I am not sure how to convert the built date string into something those functions like.

    Doing

    <xsl:variable name="startDateShow">
    9/1/2011
    </xsl:variable>

    but using any variables or parameters does not:  (actually just noticed a glaring error in my original post - the xslt should actually be the following to output defaultStartMonth, forgot the value-of in the original!)

    <xsl:variable name="startDateShow">
    <xsl:value-of select="$defaultStartMonth"/>/1/<xsl:value-of select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(),'yyyy')"/>
    </xsl:variable>

    using the first value (hard coded 9/2/2011) I can do DateAdd or DateGreaterThanOrEqual or whatever.  But using the second 'calculated' date will not let me save the xslt and gives an xslt error if you try to run it.

  • Dirk Seefeld 126 posts 665 karma points
    Aug 10, 2011 @ 23:32
    Dirk Seefeld
    0

    Hi karen,

    I'm not sure that I got you right. But I'll post a complete xslt macro file tested with version 4.7:


    DOCTYPE xsl:stylesheet [ ENTITY nbsp ""> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
      exclude-result-prefixes="msxml
    umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes
    Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings
    Exslt.ExsltSets "
    >


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:variable name="defaultStartMonth" select="09"/>

    <xsl:variable name="startDateInput" select="umbraco.library:RequestQueryString('startDate')"/>
    <xsl:variable name="endDateInput" select="umbraco.library:RequestQueryString('endDate')"/>

    <xsl:variable name="currentYear" select="Exslt.ExsltDatesAndTimes:year()"/>

        
    <xsl:variable name="sDate" select="umbraco.library:ShortDate($startDateInput)"/>
    <xsl:variable name="eDate" select="umbraco.library:ShortDate($endDateInput)"/>
       
        
    <xsl:template match="/">
      <xsl:variable name="startDate">
        <xsl:choose>
          <xsl:when test="$sDate">
            <xsl:value-of select="$sDate"/>
          xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="umbraco.library:ShortDate(concat($currentYear,'-',$defaultStartMonth,'-1'))"/>
          xsl:otherwise>
        xsl:choose>
      xsl:variable>
      
      <xsl:variable name="endDate">
        <xsl:choose>
          <xsl:when test="$eDate">
            <xsl:value-of select="$eDate"/>
          xsl:when>
          <xsl:otherwise>
             <xsl:variable name="dateStr" select="umbraco.library:ShortDate($startDate)"/>
             <xsl:if test="$dateStr">
              <xsl:value-of select="umbraco.library:DateAdd($dateStr, 'y', 1)"/>
            xsl:if>
          xsl:otherwise>
        xsl:choose>
      xsl:variable>

      <xsl:choose>
        <xsl:when test="$startDate and $endDate">
          <xsl:variable name="startDateShow" select="umbraco.library:ShortDate($startDate)"/>
          <xsl:variable name="endDateShow" select="umbraco.library:ShortDate($endDate)"/>

          startdate: <xsl:value-of select="$startDateShow"/><br />
          enddate: <xsl:value-of select="$endDateShow"/><br />
          
          <xsl:choose>
            <xsl:when test="$startDateShow and $endDateShow and umbraco.library:DateGreaterThan($endDateShow, $startDateShow)">
              startdate is before enddate
            xsl:when>
            <xsl:when test="$endDate = $startDate">
              startdate equals enddate
            xsl:when>
            <xsl:otherwise>
              startdate is after enddate
            xsl:otherwise>
          xsl:choose>

        xsl:when>
        <xsl:otherwise>
          dateformat does not match site cultur.
        xsl:otherwise>
      xsl:choose>

    xsl:template>   
    xsl:stylesheet>

    It took me a good time to figure out how to avoid all error messages. And if it is still not what you intended there might be enough hints how to use the xslt-extensions.

    Instead of a fixed date format string like 'MM/dd/yyyy' I used the shortDate() function. This will assure the right format according to the language setting for the hostname.

    Yours Dirk

    PS. @Rob: Thanks for the $-hint. The above script is updated.

  • Rob Watkins 369 posts 701 karma points
    Aug 11, 2011 @ 17:51
    Rob Watkins
    0

    You should pass XSLT dates around as yyyy-mm-dd, for starters. The .NET functions will always correctly parse that too.

    Dirk: When you set $startDate, you are missing a $ on currentYear

  • Rob Watkins 369 posts 701 karma points
    Aug 11, 2011 @ 17:56
    Rob Watkins
    0

    P.S. Fairly you will need the zero padding on that format, which you can get with:

    <xsl:variable name="fullDate">
    <xsl:value-of select="$y"/>
    <xsl:text>-</xsl:text>
    <xsl:number format="01" select="$m"/>
    <xsl:text>-</xsl:text>
    <xsl:number format="01" select="$d"/>
    </xsl:variable>

  • Rob Watkins 369 posts 701 karma points
    Aug 11, 2011 @ 17:58
    Rob Watkins
    0

    Or to be slightly less verbose:

    <xsl:variable name="fullDate">
    <xsl:value-of select="$y"/>-<xsl:number format="01" select="$m"/>-<xsl:number format="01" select="$d"/>
    </xsl:variable>

  • Dirk Seefeld 126 posts 665 karma points
    Aug 11, 2011 @ 19:16
    Dirk Seefeld
    0

    Hi Rob,

    as far as I can see a request like ... /date.aspx?startDate=2011-1-31&endDate=2011-2-3 works fine without - even leading 0 or anthing else you are mentioning;-)

    Yours Dirk

    PS: date.aspx is just a template with the corresponding macro included.

  • karen 186 posts 461 karma points
    Aug 11, 2011 @ 23:26
    karen
    0

    ok I think the keys to getting this to work is to make sure to add to the test test="$startDateShow and $endDateShow" everytime you want to do any of the date functions on one of those as well as using the ShortDate functions

    Thanks a lot!

  • Rob Watkins 369 posts 701 karma points
    Aug 12, 2011 @ 11:32
    Rob Watkins
    0

    Hi Dirk, fair enough! I did have problems with padding recently but I can't remember at all where so it was just a vague possibility :o)

Please Sign in or register to post replies

Write your reply to:

Draft