Calculating week numbers

hello,
I need to calculate what week of the year a particular date falls in, I am unsure on how to do this. After searching the forums I found the following but it seems only to resolve the current date and time. How would I apply the following code or do I do something different??

var vCalendar = java.util.Calendar.getInstance();
var vWeek = vCalendar.get(java.util.Calendar.WEEK_OF_YEAR);

Many Thanks in advance

Phil R

Worldview IT
Australia

I needed the same recently and created the following method:

var date = arguments[0];
var firstYearDay = new Date(date.getFullYear(), 0);
var day = Math.ceil((date - firstYearDay) / 86400000) + 1;
var firstDay = firstYearDay.getDay();
    
var days = firstDay + day - 2;
var week = days/7 + 1;

week = Math.floor(week)
return week;

You can also use our dateUtils-Plugin. That allows you to do all kinds of date related stuff easily. While it is finished, it lacks documentation so we did not officially announce that yet. If you are interested, drop me a line. It’s free…

The code above doesn’t always work. It has problems with week 1 vs week 52.
The DateUtils plugin from Patrick works much better :P

var vDateTime = plugins.DateUtils.DateTime(vMyDate)
var vWeek = vDateTime.weekOfYear

Thanks. And now it has documentation etc.

When using weeknumbers please take note that in the USA they count the weeknumber differently than most European countries…

The DateUtils plugin can take care of that :-) (maybe somebody from the US could confirm?)