Skip to content

Instantly share code, notes, and snippets.

@bojanrajkovic
Created November 8, 2010 16:38
Show Gist options
  • Save bojanrajkovic/667899 to your computer and use it in GitHub Desktop.
Save bojanrajkovic/667899 to your computer and use it in GitHub Desktop.
<xsl:template name="short-time">
<xsl:param name="date" /> <!-- YYYY-MM-DD HH:MM:SS +/-HHMM -->
<xsl:variable name='hour' select='substring($date, 12, 2)' />
<xsl:variable name='minute' select='substring($date, 15, 2)' />
<xsl:variable name='second' select='substring($date, 18, 2)' />
<xsl:choose>
<xsl:when test="contains($timeFormat,'H')">
<!-- 24hr format -->
<xsl:value-of select="concat($hour,':',$minute,':',$second)" />
</xsl:when>
<xsl:otherwise>
<!-- am/pm format -->
<xsl:choose>
<xsl:when test="number($hour) &gt; 12">
<xsl:value-of select="number($hour) - 12" />
</xsl:when>
<xsl:when test="number($hour) = 0">
<xsl:text>12</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$hour" />
</xsl:otherwise>
</xsl:choose>
<xsl:text>:</xsl:text>
<xsl:value-of select="$minute" />
<xsl:text>:</xsl:text>
<xsl:value-of select="$second" />
<xsl:choose>
<xsl:when test="number($hour) &gt;= 12">
<xsl:text>p</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>a</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="short-time">
<xsl:param name="date" /> <!-- YYYY-MM-DD HH:MM:SS +/-HHMM -->
<xsl:variable name='hour' select='substring($date, 12, 2)' />
<xsl:variable name='minute' select='substring($date, 15, 2)' />
<xsl:choose>
<xsl:when test="contains($timeFormat,'H')">
<!-- 24hr format -->
<xsl:value-of select="concat($hour,':',$minute)" />
</xsl:when>
<xsl:otherwise>
<!-- am/pm format -->
<xsl:choose>
<xsl:when test="number($hour) &gt; 12">
<xsl:value-of select="number($hour) - 12" />
</xsl:when>
<xsl:when test="number($hour) = 0">
<xsl:text>12</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$hour" />
</xsl:otherwise>
</xsl:choose>
<xsl:text>:</xsl:text>
<xsl:value-of select="$minute" />
<xsl:choose>
<xsl:when test="number($hour) &gt;= 12">
<xsl:text>pm</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>am</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment