0

Please help me with the code of converting given calendar date to Julian date(YYYYMMM) 7 bytes format using xslt please...

Thanks in advance !

Teja
  • 3
  • 2
  • 1
    YYYYMMM is not "Julian date". I suspect you mean the JD Edwards date format, which is YYYYDDD where DDD is the day-of-year. If so, please clarify it your processor supports XSLT 2.0. – michael.hor257k Jul 01 '19 at 18:07
  • Referring to this date format as "Julian" seems to be very common, see for example https://stackoverflow.com/questions/1171208/what-is-the-precise-definition-of-jdes-julian-date-format - but it has no connection with the Julian calendar (named after Julius Caesar) or with Julian day numbers and Julian periods introduced by Joseph Scaliger. It's a complete misnomer. – Michael Kay Jul 02 '19 at 00:04
  • Hey thanks alot for your quick response.... Yeah true it's in YYYYDDD format... And my processor doesn't support any XSLt2.0 or date/time extensions ... So only I am looking for logic on the conversion in xslt – Teja Jul 02 '19 at 11:09

2 Answers2

1

The JD Edwards date format (which is often referred to incorrectly as the JDE Julian Date format) is a CYYDDD format, where:

  • C is a number denoting the century: a value of 0 represents years between 1900 and 1999, a value of 1 years from 2000 to 2099, and so on;
  • YY denotes the year in the given century;
  • DDD is the day number in the given year.

Thus, for example, the date of 2019-07-02 would be represented as 119183.

To convert a date in YYYY-MM-DD format to JD Edwards date format using XSLT 1.0, you can use:

<xsl:template name="date-to-JDE">
    <xsl:param name="date"/>

    <xsl:variable name="year" select="substring($date, 1, 4)"/>
    <xsl:variable name="month" select="substring($date, 6, 2)"/>
    <xsl:variable name="day" select="substring($date, 9, 2)"/>

    <xsl:variable name="C" select="floor($year div 100) - 19"/>
    <xsl:variable name="YY" select="$year mod 100"/>

    <xsl:variable name="leap" select="not($year mod 4) and $year mod 100 or not($year mod 400)" />
    <xsl:variable name="elapsed-months" select="substring('000031059090120151181212243273304334', 3 * ($month - 1) + 1, 3)"/>
    <xsl:variable name="DDD" select="$elapsed-months + ($month > 2 and $leap) + $day"/> 

    <xsl:value-of select="$C * 100000 + $YY * 1000 + $DDD"/>            
</xsl:template>

Demo: https://xsltfiddle.liberty-development.net/jyRYYj8


Added:

If you actually need YYYYDDD, then you can simplify the above to:

<xsl:template name="date-to-YYYYDDD">
    <xsl:param name="date"/>

    <xsl:variable name="year" select="substring($date, 1, 4)"/>
    <xsl:variable name="month" select="substring($date, 6, 2)"/>
    <xsl:variable name="day" select="substring($date, 9, 2)"/>

    <xsl:variable name="leap" select="not($year mod 4) and $year mod 100 or not($year mod 400)" />
    <xsl:variable name="elapsed-months" select="substring('000031059090120151181212243273304334', 3 * ($month - 1) + 1, 3)"/>
    <xsl:variable name="DDD" select="$elapsed-months + ($month > 2 and $leap) + $day"/> 

    <xsl:value-of select="$year * 1000 + $DDD"/>            
</xsl:template>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Thanks again for your quick response. But I am looking for YYYYDDD - 7 bytes format. For example , if given date is 1986-01-15 - need to convert to 1986015.Please guide us. – Teja Jul 02 '19 at 12:52
  • @Teja If you want YYYYDDD (where YYYY is the 4-digit year, and DDD is the day number in the given year), then just change the final `xsl:value-of` to `` – Tim C Jul 02 '19 at 15:51
  • @Tim.. Yeah used the same by modifying a little bit & generated. I need to calculate the day of the year..So I had taken that from the above code .. Thanks alot! it worked... – Teja Jul 02 '19 at 16:35
0

In XSLT 2.0, format-date() with picture [Y0001][d001] should give you what you want.

If you can't use XSLT 2.0, see whether your XSLT processor supports the date/time extensions defined at www.exslt.org

Michael Kay
  • 156,231
  • 11
  • 92
  • 164