Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 16th, 2004, 05:32 AM
Registered User
 
Join Date: Mar 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Formatting a Field Containing a Time to AM/PM

Trying to write the XSL that displays tracking results from UPS, and I have it ALMOST done, except for one thing. In the following:

<td bgcolor="ffffff" align="center">

        <xsl:value-of select="Time"/>

</td>


Time is a 6digit, 24 hour time. So 3:18:23 PM is displayed as: 151823, 4:12:01 AM would be 041201 - I WANT it to display in AM/PM (and it can ignore the seconds, no problem)... I keep looking for something "built-in" that I can just say "format this into 12-hour time" but, being new to XML and XSL, I'm not having much luck. Can someone please show me how to re-write the above to display the time in AM/PM instead of the 6-digit number it displays now?

Any help would be greatly appreciated!!


 
Old March 16th, 2004, 06:52 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Add this named template to your stylesheet:
Code:
 <xsl:template name="FormatTime">
    <xsl:param name="timeString"/>
    <xsl:variable name="hours" select="number(substring($timeString, 1, 2))"/>
    <xsl:variable name="minutes" select="number(substring($timeString, 3, 2))"/>
    <xsl:variable name="seconds" select="number(substring($timeString, 5, 2))"/>
    <xsl:variable name="indicator">
      <xsl:choose>
        <xsl:when test="$hours &gt; 11">PM</xsl:when>
        <xsl:otherwise>AM</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="modifiedHours">
      <xsl:choose>
        <xsl:when test="$hours = 0 or $hours = 12"><xsl:value-of select="12"/></xsl:when>        
        <xsl:otherwise><xsl:value-of select="($hours) mod 12"/></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="modifiedMinutes">
      <xsl:choose>
        <xsl:when test="$seconds &gt; 29"><xsl:value-of select="$minutes + 1"/></xsl:when>
        <xsl:otherwise><xsl:value-of select="$minutes"/></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:value-of select="concat($modifiedHours, ':', $modifiedMinutes, ' ', $indicator)"/>
  </xsl:template>
Change your existing code to this:
Code:
<td bgcolor="ffffff" align="center">

        <xsl:call-template name="FormatTime">
          <xsl:with-param name="timeString" select="Time"/>
        </xsl:call-template>

</td>
Needs to be tested:)

Joe (MVP - xml)
 
Old March 16th, 2004, 11:10 PM
Registered User
 
Join Date: Mar 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Worked Perfect! Thank you so much!!!!!

Now if I can just figure out how to take the XML data and get it into a format I can input into my database. I need like ONE field: RatingServiceSelectionResponse/RatedShipment/RatedPackage/TotalCharges/MonetaryValue





Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting Time Output braunj ASP.NET 2.0 Basics 0 October 6th, 2008 10:27 PM
formatting integer field gloribel Crystal Reports 1 October 13th, 2005 12:27 PM
formatting date time Adam H-W Classic ASP Basics 4 September 29th, 2004 03:35 AM
Time conversions from 24hr to am/pm Blaise Classic ASP Basics 2 July 14th, 2003 09:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.