Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate weeknumber from a date value

Tags:

date

xml

xslt

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 :(

like image 321
Toni Frankola Avatar asked Oct 07 '08 14:10

Toni Frankola


People also ask

How do I calculate the week number given a date?

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 do I convert a date range to a week number in Excel?

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).

How do I calculate week number in Excel?

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.


2 Answers

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.

like image 196
James Sulak Avatar answered Sep 22 '22 23:09

James Sulak


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.

like image 30
Mark Ransom Avatar answered Sep 23 '22 23:09

Mark Ransom