Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert text to date, then get year value?

Need to convert a text value `2012-03-19' into a date type, then extract the year component.

<xsl:variable name="dateValue" select="2012-03-19"/>
<xsl:variable name="year" select="year-from-date(date($dateValue))"/>

I'm using Saxon 2.0, but it's complaining date function does not exist; I looked around Saxon's documentation and could not find the function, so that's clearly the issue, but I can't find a suitable replacement.

like image 856
raffian Avatar asked Mar 16 '12 21:03

raffian


People also ask

How do I pull a year from text in Excel?

To extract the year from a cell containing a date, type =YEAR(CELL) , replacing CELL with a cell reference. For instance, =YEAR(A2) will take the date value from cell A2 and extract the year from it.

How do I convert a date to a year number in Excel?

1. Select a blank cell adjacent to the cell you want to display year of date only, and then type formula =YEAR(A1) into the Formula Bar, and then press the Enter key to get the result.


1 Answers

I don't think date() should be a function, you want the xs:date() data type.

Add the xs namespace and then prefix xs:date().

The following stylesheet, using any well-formed XML input, will produce 2012:

<xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <xsl:variable name="dateValue" select="'2012-03-19'"/>
    <xsl:variable name="year" select="year-from-date(xs:date($dateValue))"/>
    <xsl:value-of select="$year"/>
  </xsl:template>

</xsl:stylesheet>

Note that you also have to quote your select in your "dateValue" xsl:variable.

like image 187
Daniel Haley Avatar answered Oct 02 '22 06:10

Daniel Haley