smr5
smr5

Reputation: 2793

Format Date and Time in XSLT

I have the column which returns the date and time in the default format.

<xsl:value-of select="/CAudioFile/CRI/LocalStartTime"/>

Which returns

2014-05-08T08:01:26.4000000-0700

I want to put this time in this format:

05/08/2014 08:01:26

I have tried to get the format with the different combination of substring:

<xsl:value-of select="substring(/CAudioFile/CRI/LocalStartTime,1,10)"/>

But it's either returning me just the date or just the time.

Any suggestions?

Thank you.

Upvotes: 1

Views: 15062

Answers (3)

Daniel Haley
Daniel Haley

Reputation: 52858

You didn't specify what version of XSLT. If you can use 2.0, you can use format-dateTime().

In your case it's a little tricky though because your example isn't castable as an xs:dateTime because of the timezone. The timezone needs to be 2 digits; 07:00 instead of 0700. You can fix this by using replace() and casting as an xs:dateTime.

Here's an example. I've put the replace() in an xsl:variable to make it easier to read. It isn't required though.

XML Input

<test>2014-05-08T08:01:26.4000000-0700</test>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:variable name="dt" select="xs:dateTime(replace(normalize-space(.),'([+-]\d{2})(\d{2})$','$1:$2'))" as="xs:dateTime"/>
        <xsl:copy>
            <xsl:value-of select="format-dateTime($dt,'[M00]/[D00]/[Y] [H00]:[m00]:[s00]')"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Output

<test>05/08/2014 08:01:26</test>

You can find more info on the picture string (second arg of format-dateTime()) here:

http://www.w3.org/TR/xslt20/#date-picture-string

Upvotes: 2

helderdarocha
helderdarocha

Reputation: 23637

These variables will give you the date (in ISO 8601 format) and the time using XPath 1.0 expressions:

<xsl:variable name="date">
    <xsl:value-of select="substring-before(.,'T')"/>
</xsl:variable>
<xsl:variable name="time">
    <xsl:value-of select="substring-before(substring-after(.,'T'),'.')"/>
</xsl:variable>

To convert your date to the mm/dd/yyyy format, you can add this template to your stylesheet:

<xsl:template name="iso-to-mdy">
    <xsl:param name="iso-date"/>
    <xsl:variable name="year" select="substring($iso-date,1,4)"/>    
    <xsl:variable name="month" select="substring($iso-date,6,2)"/>
    <xsl:variable name="day" select="substring($iso-date,9,2)"/>
    <xsl:value-of select="$month"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$day"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$year"/>
</xsl:template>

And instead declare your date variable like this, passing your ISO 8601 date as a parameter:

<xsl:variable name="date">
    <xsl:call-template name="iso-to-mdy">
        <xsl:with-param name="iso-date" select="substring-before(.,'T')"/>
    </xsl:call-template>
</xsl:variable>

Then you will have the date in the format you want. To print the space between time and date you can use <xsl:text>:

<date>
    <xsl:value-of select="$date"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$time"/>
</date>

which will print

<date>05/08/2014 08:01:26</date>

See XSLT Fiddle

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 116982

You need to work a bit harder at it:

<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 6, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 9, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 1, 4)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 12, 8)"/>

Upvotes: 4

Related Questions