Day of Year in XSL Sheet

weklica
weklica used Ask the Experts™
on
I have the following XSL which gives me 'dow' as 0-6 which I can call later on (see below).
I need the same sort of code which will also allow me to call 'doy' for day of year.  So, October 31st would result as 305 or something similar.  January 1st would be a 1.  

Any guidance on this would be great.  
Thanks

	<xsl:template match="/dataset">
		<dataset>
			<xsl:variable name="year" select="number(substring($date, 1, 4)) "/>
			<xsl:variable name="month" select="number(substring($date, 5, 2)) "/>
			<xsl:variable name="day" select="number(substring($date, 7, 2)) "/>
			<xsl:variable name="a" select="floor((14 - $month) div 12)"/>
			<xsl:variable name="y" select="$year - $a"/>
			<xsl:variable name="m" select="$month + 12 * $a - 2"/>
			<!-- 0-6, where 0=Sunday. -->
			<xsl:variable name="dow" select="($day + $y + floor($y div 4) - floor($y div 100) + floor($y div 400) + floor((31 * $m) div 12)) mod 7"/>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Try this:


There are also some good functions shown on this page, and while one doesn't do exactly what you need, it would be easy to use the same technique as above and get the Julian day number, and subtract the Julian day number of the 1st day of that year, then add 1.  Take a look at the section "4.4. Calculating Julian and Absolute Day Numbers from a Specified Date" as a starting point.



»bp
Zvonko זְאֵבSystems architect
Top Expert 2006

Commented:
Here my proposal:
<xsl:template match="/">
		<dataset>
			<xsl:variable name="date" select="//dataset/date" />
			<xsl:variable name="year" select="number(substring($date, 1, 4)) "/>
			<xsl:variable name="month" select="number(substring($date, 5, 2)) "/>
			<xsl:variable name="day" select="number(substring($date, 7, 2)) "/>
			<xsl:variable name="a" select="floor((14 - $month) div 12)"/>
			<xsl:variable name="y" select="$year - $a"/>
			<xsl:variable name="m" select="$month + 12 * $a - 2"/>
			<!-- 0-6, where 0=Sunday. -->
			<xsl:variable name="dow" select="($day + $y + floor($y div 4) - floor($y div 100) + floor($y div 400) + floor((31 * $m) div 12)) mod 7"/>			
			<xsl:variable name="n1" select="floor((275 * $month) div 9)"/>			
			<xsl:variable name="n2" select="floor(($month + 9) div 12)"/>
			<xsl:variable name="n3" select="(1 + floor(($year - 4 * floor($year div 4) + 2) div 3))"/>
			<!-- 0-356, days since January, 1st -->
			<xsl:variable name="doy" select="$n1 - ($n2 * $n3) + $day - 31"/>

Open in new window

The formula name is Zeller's congruence:
https://astronomy.stackexchange.com/questions/2407/calculate-day-of-the-year-for-a-given-date
Systems architect
Top Expert 2006
Commented:
sorry, the comment is: 0-365  of course  <|:-)

Author

Commented:
Wonderful. I will test this out tonight when I am near the station. Thanks much!
Zvonko זְאֵבSystems architect
Top Expert 2006

Commented:
Hello weklica, did you have the opportunity to test my formula?

Author

Commented:
Zvonko's solution was exceedingly helpful!   Thanks much!!!!
Zvonko זְאֵבSystems architect
Top Expert 2006

Commented:
You are welcome  :  )

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial