Jump to content

Check if Date equals Today's Date


Online Services
 Share

Recommended Posts

I have the following XSL stylesheet which displays a weather forecast on our SharePoint 2010 site. It displays today and tomorrow's weather (see screenshot attached). What I want to do is add the words 'Today:' next to today's date and 'Tomorrow:' next to tomorrow's date. Does anybody know how I could do that? I am only a newbie at XSL and somebody else wrote the following code for me.

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"	xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">  <xsl:output method="xml" indent="yes"/>  <xsl:template match="/">	<html>	  <head></head>	  <body>		<xsl:for-each select="Data/DataItems/Location">		  <h2>			<xsl:value-of select="." />		  </h2>		  <p class="last-updated">			<xsl:variable name="issuedOn">			  <xsl:call-template name="formatDate">				<xsl:with-param name="date" select="following-sibling::Issued[1]" />			  </xsl:call-template>			</xsl:variable>			Issued:			<xsl:value-of select="$issuedOn" />		  </p>		  <p>			<xsl:value-of select="following-sibling::Warning[1]" />		  </p>		  <p class="last-updated">			<xsl:variable name="validUntil">			  <xsl:call-template name="formatDate">				<xsl:with-param name="date" select="following-sibling::ValidUntil[1]" />			  </xsl:call-template>			</xsl:variable>			This warning is valid until:			<xsl:value-of select="$validUntil" />		  </p>		</xsl:for-each>	  </body>	</html>  </xsl:template>   <!-- ## Custom date template ## -->  <xsl:template name="formatDate">	<xsl:param name="date" />	<xsl:variable name="day" select="substring($date, 7, 2)" />	<xsl:variable name="year" select="substring($date, 1, 4)" />	<xsl:variable name="hrs" select="substring($date, 9, 4)" />	<xsl:variable name="month">	  <xsl:variable name="monthNumber" select="substring($date, 5, 2)" />	  <xsl:choose>		<xsl:when test="$monthNumber = 1">January</xsl:when>		<xsl:when test="$monthNumber = 2">February</xsl:when>		<xsl:when test="$monthNumber = 3">March</xsl:when>		<xsl:when test="$monthNumber = 4">April</xsl:when>		<xsl:when test="$monthNumber = 5">May</xsl:when>		<xsl:when test="$monthNumber = 6">June</xsl:when>		<xsl:when test="$monthNumber = 7">July</xsl:when>		<xsl:when test="$monthNumber = 8">August</xsl:when>		<xsl:when test="$monthNumber = 9">September</xsl:when>		<xsl:when test="$monthNumber = 10">October</xsl:when>		<xsl:when test="$monthNumber = 11">November</xsl:when>		<xsl:when test="$monthNumber = 12">December</xsl:when>	  </xsl:choose>	</xsl:variable>	<xsl:value-of select="concat($day, ' ', $month, ' ', $year, ' at ', $hrs, ' hrs' )"/>  </xsl:template></xsl:stylesheet>

post-91249-0-56687100-1328645924_thumb.gif

Link to comment
Share on other sites

There aren't any built in functions for date checking in XSLT 1.0. There are in XSLT 2.0, but that's not supported by MSXML, which I assume you're using in one form (IE) or another (ASP(.NET)).You could import and use EXSLT's difference() function and then check if the difference is less than 24*60*60 (today) or 2*24*60*60 (tomorrow).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...