3 Replies Latest reply on Jul 15, 2019 7:05 AM by Martien van den Akker

    How to convert m/d/yyyy to mm/dd/yyy

    3426083

      Hi All,

       

      I am getting input in csv file like 6/9/2019 12:50:30 AM. and I need to convert it to 2019-06-09T12:50:30 (yyyy-mm-ddThh:mm:ss)

       

      I am facing issue while doing substring and then concat the string.

      There are possibilities of input like 12/9/2019 12:50:30 AM ,6/24/2019 12:50:30 AM & also 11/31/2019 12:50:30 AM.

      Below mentioned substring function is not working as the input is not in well-defined format.

       

      So is there any way we can append zero before doing substring?

      Please suggest.

       

      Here DateTimeStr =6/9/2019 12:50:30 AM

               datestr=6/9/2019

       

      <xsl:template name="formatdate">

          <xsl:param name="DateTimeStr"/>

          <xsl:variable name="datestr" select="substring-before($DateTimeStr,' ')" />

         

         <xsl:variable name="timestr">

            <xsl:value-of select="substring-after($DateTimeStr,' ')"/>

          </xsl:variable>

          <xsl:variable name="mm">

            <xsl:value-of select="substring($datestr,1,2)"/>

          </xsl:variable>

          <xsl:variable name="dd">

            <xsl:value-of select="substring($datestr,4,2)"/>

          </xsl:variable>

          <xsl:variable name="yyyy">

            <xsl:value-of select="substring($datestr,7,4)"/>

          </xsl:variable>

       

          <xsl:variable name="hh">

            <xsl:value-of select="substring($timestr,1,2)"/>

          </xsl:variable>

          <xsl:variable name="MM">

            <xsl:value-of select="substring($timestr,4,2)"/>

          </xsl:variable>

          <xsl:variable name="ss">

            <xsl:value-of select="substring($timestr,7,2)"/>

          </xsl:variable>

          <xsl:value-of select="concat($yyyy,'-',$mm,'-',$dd,'T',$hh,':',$MM,':',$ss)"/>

        </xsl:template>

       

      Appreciate your earliest help here.

      Thanks in advance.

        • 1. Re: How to convert m/d/yyyy to mm/dd/yyy
          Martien van den Akker

          Hi,

           

          What I do in these cases is prepend with a '0' and then do a substring. https://stackoverflow.com/questions/25662151/padding-number-with-leading-zeros-in-xslt-1-0  explains the idea.

           

          Regards,
          Martien

          • 2. Re: How to convert m/d/yyyy to mm/dd/yyy
            3426083

            Thanks for your reply.

             

            Problem here I can't always prepend '0' only when date or month is single digit.

            example in case of 12/29/2010

             

            I have appended using choose option and it is working fine now.

             

            <xsl:template name="formatdate">

              <xsl:param name="DateTimeStr"/>

                <xsl:variable name="datestr" select="substring-before($DateTimeStr,' ')" /> 

                <xsl:variable name="timestr" select="substring-after($DateTimeStr,' ')"/>

                <xsl:variable name="mmstr" select="substring-before($datestr,'/')"/> 

                <xsl:variable name="ddstr" select="substring-before(substring-after($datestr,'/'),'/')"/>

                   

                <xsl:variable name="mm">

                    <xsl:choose>   

                        <xsl:when test="string-length($mmstr)=1 ">

                              <xsl:value-of select="concat(0,$mmstr)"/>

                        </xsl:when>

                        <xsl:otherwise>

                              <xsl:value-of select="$mmstr"/>

                        </xsl:otherwise>

                    </xsl:choose>

                </xsl:variable>

             

            This is working as per expectation.

             

            Thanks

            • 3. Re: How to convert m/d/yyyy to mm/dd/yyy
              Martien van den Akker

              Problem here I can't always prepend '0' only when date or month is single digit.

              Of course, that's why you have to do it using a smart substring. So you always prepend with a zero and then get the last two digits.

              But a choose like this works fine too.

               

              Regards,
              Martien