When I use the XML serializer to serialize a DateTime
, it is written in the following format:
<Date>2007-11-14T12:01:00</Date>
When passing this through an XSLT stylesheet to output HTML, how can I format this? In most cases I just need the date, and when I need the time I of course don't want the "funny T" in there.
Can use the following conversion code to convert UTC format string to any other DateTime format. string result = Convert. ToDateTime("2011-02-04T00:00:00+05:30"). ToString("MM/dd/yyyy h:mm:ss tt");
XSLT current() Function The current() function returns a node-set that contains only the current node. Usually the current node and the context node are the same.
XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.
The regex-group() function names which matched string you want to use inside of the xsl:matching-substring element; pass it a 1 to get the first, a 2 to get the second, and so forth. The example above uses it to plug the three matched values inside new city, state, and zip elements created for the output.
Here are a couple of 1.0 templates that you can use:-
<xsl:template name="formatDate"> <xsl:param name="dateTime" /> <xsl:variable name="date" select="substring-before($dateTime, 'T')" /> <xsl:variable name="year" select="substring-before($date, '-')" /> <xsl:variable name="month" select="substring-before(substring-after($date, '-'), '-')" /> <xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')" /> <xsl:value-of select="concat($day, ' ', $month, ' ', $year)" /> </xsl:template> <xsl:template name="formatTime"> <xsl:param name="dateTime" /> <xsl:value-of select="substring-after($dateTime, 'T')" /> </xsl:template>
Call them with:-
<xsl:call-template name="formatDate"> <xsl:with-param name="dateTime" select="xpath" /> </xsl:call-template>
and
<xsl:call-template name="formatTime"> <xsl:with-param name="dateTime" select="xpath" /> </xsl:call-template>
where xpath is the path to an element or attribute that has the standard date time format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With