Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string to Date format in XSLT

I have a date(string) value in an XML file in this format:

Tue Apr 17 03:12:47 IST 2012

I want to use XSL transformation to convert the string/date into this format:

4/17/2012 03:12:47 AM

How can I do that in my XSL transform?

like image 424
Rg90 Avatar asked Jun 03 '13 08:06

Rg90


People also ask

How can I change the date format in XML?

Can use the following conversion code to convert UTC format string to any other DateTime format. string result = Convert. ToDateTime("2011-02-04T00:00:00+05:30"). ToString("MM/dd/yyyy h:mm:ss tt");

How do I substring in XSLT?

Inputs. The substring() function takes a string and one or two numbers as arguments. The string is the string from which the substring will be extracted. The second argument is used as the starting position of the returned substring, and the optional third argument specifies how many characters are returned.

How do I replace in XSLT?

The Replace function works by writing an XSLT template for this function and calls this replace whenever replacement of the string action is required. This function is very similar to regex-group ().


1 Answers

If you are using

  • XSLT 1.0 version, use EXSLT - date:format-date date extension

  • XSLT 2.0 version, use built-in: Formatting Dates and Times date extension

But my suggestion is to

Have a standard XSD datetime format on XML, on the code-behind (that is, on rendering time) you can format as you like.

Update:

Always XML to process through XSLT, dates should be in standard XSD format. Currently your input is not in standard format so that it throws error.

Example:

<xsl:variable name="dt" as="xs:dateTime" select="xs:dateTime('2012-10-21T22:10:15')"/>
<xsl:value-of select="format-dateTime($dt, '[Y0001]/[M01]/[D01]')"/>

OUTPUT:

2012/10/21

like image 80
Siva Charan Avatar answered Oct 03 '22 06:10

Siva Charan