Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the time from a dateTime value?

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

I saw there was fn:hours-from-dateTime(datetime) thanks to w3schools. So I'm wondering, why is there no fn:time-from-dateTime(datetime)?

And how do I use it? My current code is :

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

Which display the dateTime correctly. However :

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

Doesn't 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>

?

like image 236
Kraz Avatar asked Jan 19 '23 11:01

Kraz


2 Answers

Just use a cast or constructor function:

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

This assumes that attribute @dateTime is of type xs:dateTime as a result of schema processing. If you're not running a schema-aware processor, you'll need to cast it to xs:dateTime first:

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

and of course that space between the date and time needs to be a "T" for this to work.

like image 90
Michael Kay Avatar answered Jan 26 '23 00:01

Michael Kay


Thanks for both suggestion, but since I didn't really need it as a dateTime variable, I just treated it as a string and used :

<xsl:value-of select="substring-after(@EventDate, ' ')"/>
like image 32
Kraz Avatar answered Jan 26 '23 00:01

Kraz