# Thread: Count the weeks within the month? help.

1. Member
Join Date
Jul 2010
Posts
31

## Count the weeks within the month? help.

Hey everybody, okay??

I need to count the weeks of the month within the year and I am having the following problem.

I'm using the function:

getDayNumber (start_date, "wm");

The problem is that this counting function of 7 in 7 days.

Example: In January 2010 until 1 week had only two days since the year began on Friday that it counts only up to 08 days to overturn the 2nd week.

has some way to tell it correctly?

2. Senior Member
Join Date
Nov 2008
Posts
777
If you are working with a database then MySQL has date functions that would work. If not, then my advice is to use java.util.Calendar.

3. Chief Data Integration
Join Date
Nov 1999
Posts
9,729
I never heard of counting the weeks of the months within a year.
Clearly tell us what you are trying to accomplish first, then we'll advise.

4. Member
Join Date
Jul 2010
Posts
31
ok.

I must send to the database (PostgreSQL) the following information:

Start Date
Final Date
Total days between two dates
Total hours between two dates
Weekdays (1, 2, 3, 4, 5, 6 or 7)
Days of the year (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ......, 365)
Weeks of the month (1, 2, 3, 4 or 5)
Weeks of the year (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ......, 52)

My only problem is I can not count the number of weeks within the month.

Thank you.

5. Pentaho Enterprise Architect
Join Date
Sep 2009
Posts
810
To get all of this you should probably use the java calendar, since it gives you most control over how weeks are calculated. Week calculations vary between cultures and organizations. Some people's weeks start on sunday, other people's weeks start on monday. Likewise it is a matter of cultural preference as to the weeks during the transition to a new year. There's ISO http://en.wikipedia.org/wiki/ISO_8601 and there's local preferences that vary. Same difficulties apply when counting weeks in months.

For this reason I'd like to suggest you use the java.util.Calendar class

If you need help working with it I suggest you check out this article. It explains how to use java classes from within kettle's JS step.

Here's a short sample on how it can be used in a JS step, supposing year, month, and day are coming in as fields:

Code:
```var Calendar = Packages.java.util.Calendar;

var quarter = Math.floor((month-1) / 3) +1;
var quarter_label = 'Q'+quarter;

var cal = Calendar.getInstance();
cal.set(year, month-1, day);

var days_in_year = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
var is_leap_year = (days_in_year == 366)?1:0;

var week_of_month = cal.get(Calendar.WEEK_OF_MONTH);```
Remember that you can (and probably should) specify a locale when calling Calendar.getInstance() to unambiguously specify the way weeks are handled.

Cheers

Slawo

6. Chief Data Integration
Join Date
Nov 1999
Posts
9,729
Pretty impressive Slawo! I wouldn't have known where to start.
A new question comes to mind though... Santi, what kind of use does the number of weeks in a month have? What can you do with it?

7. Member
Join Date
Jul 2010
Posts
31
Ok guys, I'll try to adapt.

MattCasters: In my case I need to provide bank information to a manager and so I work with virtually all types of information.

Sorry for my English, I'm using Google translator lol

Thanks

8. Junior Member
Join Date
Feb 2010
Posts
2
Originally Posted by MattCasters
Pretty impressive Slawo! I wouldn't have known where to start.
A new question comes to mind though... Santi, what kind of use does the number of weeks in a month have? What can you do with it?
Its quite usefull. For example, in retail industry, most customer will earn their salary in 1st week of the month, so comparing sales figure in monthly base or even weekly based, is not reflect the actual trend. The best way is compare 1st week on current month with several pass month at the same week of month number.