Is there some simple way to calculate a Weeknumber value from a date value stored in XML?
It needs to be pure XSLT solution. I cannot use any code :(
The WEEKNUM function takes a date and returns a week number (1-54) that corresponds to the week of year. The WEEKNUM function starts counting with the week that contains January 1. WEEKNUM takes two arguments: a date, and (optionally) return_type, which controls the scheme used to calculate the week number.
How to get the date from a week number. To get the date of the Monday in a week, use =DATE( A1 , 1, -3 + 7 * B1 - WEEKDAY(DATE( A1 , 1, 4), 2) + 1) . Cell A1 contains the four-digit year (e.g. 2022), and cell B2 contains the week number (1-53).
The WEEKNUM function takes a date and returns a number between 1 and 54 that corresponds to the week of the year. By default, the WEEKNUM function starts counting on the week that contains January 1 and increments week numbers on Sunday. The WEEKNUM function accepts two arguments, serial_num and return_type.
If you can use EXSLT, there are several date functions available. All of them are implemented in Saxon, but if you're using MSXSL, Chris Bayes has implemented them as extension functions, which you can actually place within your transform inside an msxsl:script element. His implementations are linked from each specific date function page.
Is week-in-year() the function you're looking for?
Edit: Per JeniT's comment, there's a pure XSLT 1.0 template available on the same site with the same functionality as week-in-year() (which she wrote, I think), which might fit your requirements better.
Week calculation can get pretty complicated if you always want the week to start on the same day, because the first day of the year is always changing. There's an ISO standard for calculating it, see this Wikipedia article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With