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)
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.
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.
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!)
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.
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.
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.
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
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)
This will let me save the xslt file.
Next I want to get an end date by adding 1 year, so trying this:
but this won't let me save, I get an error (doesn't give a line number)
The error is occuring on the DateAdd because if I take that line out, the error goes away.
The following does work
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
but get the same error.
Any suggestions?
One more note, if I use current date instead of trying to build a date, everything works fine:
Hi karen,
try this:
Greetings Dirk
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.
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.
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
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!)
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.
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:
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.
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
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>
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>
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.
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!
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)
is working on a reply...