How to convert 12 hour format time in to 24 hour Format as a Property in the WSO2 ESB
sample time : 6:00PM
Converted time : 1800
How to convert 12 hour format time in to 24 hour Format as a Property in the WSO2 ESB
sample time : 6:00PM
Converted time : 1800
WSO2 does not have an OOB solution for this. But there are multiple ways to implement this.
You can use the following XSLT to convert your time to 24Hours format. Note that the following XSLT is tightly coupled to the sample formats you provided. If you have a different format you may need to alter the XSLT accordingly.
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="LOCAL_XSLT_Time" xmlns="http://ws.apache.org/ns/synapse">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="yes"/>
<xsl:param name="timeToConv"/>
<xsl:template match="/">
<!-- Iterating through message elements -->
<time>
<xsl:call-template name="time-to-24H">
<xsl:with-param name="timeToConvert" select="$timeToConv"/>
</xsl:call-template>
</time>
</xsl:template>
<xsl:template name="time-to-24H">
<xsl:param name="timeToConvert"/>
<xsl:variable name="hours" select="substring-before($timeToConvert, ':')"/>
<xsl:variable name="minWithComp" select="substring-after($timeToConvert, ':')"/>
<xsl:variable name="min" select="substring($minWithComp, 1, string-length($minWithComp) - 2)"/>
<xsl:variable name="comp" select="substring($minWithComp, string-length($minWithComp) - 1, string-length($minWithComp))"/>
<xsl:variable name="hourPart" select="floor($hours mod 12 + 12 * number($comp='PM'))"/>
<xsl:value-of select="$hourPart"/>
<xsl:value-of select="$min"/>
</xsl:template>
</xsl:stylesheet>
</localEntry>
You can invoke this XSLT using the XSLT mediator. Read more on the XSLT mediator from here.
<xslt key="LOCAL_XSLT_Time">
<property name="timeToConv" value="6:00PM"/>
</xslt>
Then after the XSLT mediator, you can access converted time like below.
<property name="convertedTime" expression="//time/text()"></property>
This question has more details on how to use the XSLT mediator.
2 Using the script mediator.
You can use the Script mediator with JavaScript to convert your time. There are plenty of JavaScript samples out there for this. You can refer this to understand how to use the script mediator.