How to get time from dateTime value?

I have XML attributes, <EventDate>2011-06-16 08:00:00</EventDate> and I want to extract 08:00:00 using XSLT.

I saw what was fn:hours-from-dateTime(datetime) thanks to w3schools . So I wonder why not fn:time-from-dateTime(datetime) ?

And how to use it? My current code is:

 <td><xsl:value-of select="@EventDate"/></td> 

How to correctly display the date. But:

  <td><xsl:value-of select="hours-from-dateTime(@EventDate)"/></td> 

Does not work.

Finally, is there something more elegant than doing:

 <td><xsl:value-of select="hours-from-dateTime(@EventDate)"/>: <xsl:value-of select="minutes-from-dateTime(@EventDate)"/>: <xsl:value-of select="seconds-from-dateTime(@EventDate)"/></td> 

?

+4
source share
3 answers

Thanks for both of the suggestions, but since I really didn't need it as a dateTime variable, I just looked at it as a string and used:

 <xsl:value-of select="substring-after(@EventDate, ' ')"/> 
+2
source

Just use the cast or constructor function:

 <xsl:value-of select="xs:time(@dateTime)"/> 

This assumes that the @dateTime attribute is of type xs: dateTime as a result of processing the schema. If you are not using a circuit-oriented processor, you first need to port it to xs: dateTime:

 <xsl:value-of select="xs:time(xs:dateTime((@dateTime))"/> 

and, of course, that the space between date and time must be β€œT” for this.

+5
source

The date you start with is missing the 'T' time separator, so you need to insert it if you want to use the dateTime functions.

I suggest the following:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs fn" version="2.0" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:template match="/"> <foo> <xsl:variable name="time" select="translate(string(/input), ' ', 'T')"/> <xsl:value-of select="fn:format-dateTime(xs:dateTime($time), '[h]:[m01]:[s01]')"/> </foo> </xsl:template> 

This assumes input as follows:

 <input>2011-06-16 09:00:00</input> 

For more information, see the format-dateTime function in the xslt20 specification. There are many different options for formatting a date time value using the image string argument.

+1
source

All Articles