Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find how many weeks in a year

Status
Not open for further replies.

level1

Programmer
Apr 16, 2002
60
GB
Dear all

I ve been asked to do find a way to calculate all weeks in a year.

More specifically working weeks, which doesnt mean that i have to exlude any days like weekends but simply it means that the first week it will be: 14-March-2002 to 20-March-2002 starting from Thursday to Wednesday. So all these working weeks will count standart Thursday to the following Wednesday.

And as that the last working week it will be:
6-Mar-2003 to 12-Mar-2003

In total we have 52 working weeks.

Al i wanted was a similar script that given a Date will display a number (from 1-52) that shows in which working week we are.

So for example if i give 14-Mar-2002 the script will return 1.
and also if i give 17-Mar-2002 the script will again return 1. While giving 12-Mar-2003 the script will return 52.

The problem is that i did that using ASP script which goes through every single week and validate it as follows:

if (days>=14) and (days<=20) AND (months=&quot;Mar&quot;) AND (years=&quot;2002&quot;) THEN : i = 1 : end if
if (days>=21) and (days<=27) AND (months=&quot;Mar&quot;) AND (years=&quot;2002&quot;) THEN : i = 2 : end if

if ((days>=28) and (months=&quot;Mar&quot;)) OR ((days<=3) and (months=&quot;Apr&quot;)) AND (years=&quot;2002&quot;) THEN : i = 3 : end if Rem***month changes

.......

if ((days>=26) and (months=&quot;Dec&quot;) and (years=&quot;2002&quot;)) OR ((days<=1) and (months=&quot;Jan&quot;) and (years=&quot;2003&quot;)) THEN : i = 42 : end if rem year changes

.......

if (days>=6) and (days<=12) AND (months=&quot;Mar&quot;) AND (years=&quot;2003&quot;) THEN : i = 52 : end if

This done on ASP and works fine. But the problem is that only works for hardcoded dates as you see. E.g cannot work for year 2005 cause i havent put anything for this.

So i was about a Js or VB script that can do something similar but more dinamiclly. Any thoughts?
 
I have kindof used this before

Its in ASP, incase you were wondering

Basically i just used this to test

weekno = datediff(&quot;w&quot;,formatdatetime(&quot;14/03/2002&quot;),formatdatetime(date))

Weekno is the variable i'm putting the number into

datediff(,,) is an asp function

&quot;w&quot; tells it to count weeks (this works for d-days and m-months!)

Where i have put 14/03/2002 you could maybe put a variable that works out previously if date is > 14/03/2002 its 2002 and if its greater than 13/03/2003 then its 2003 kinda thing,

Then i just used todays date which again you can use any variable you want, with the above i got '4' which works out correct.

Hope this helps, its my first real answer to a question that i actually understand

Cheers
Dave

Theres nothing worse than an idea when its the only one we have!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top