473,289 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

calculate weeknumber and period

Hello All,
I would like to calculate weeknumber and period from first day of the week (sunday) and last day of the week (saturday) and week number falls from 1 to 52 only. If week number >= 53 then week number will equal to 52. And Last Day of First week is the first Saturday of January, as long as it falls at least four days into the month
Example:
if enter
Date WeekNo. Period
12/31/06 1 12/31/06-01/06/07
01/05/07 1 12/31/06-01/06/07
01/09/07 2 01/07/07-01/13/07
12/24/07 52 12/23/07-12/29/07
12/30/07 1 12/30/07-01/05/08
12/29/08 52 12/28/08-01/03/09
01/05/09 1 01/04/09-01/10/09

Any help would be appreciated. Thanks alots.

Codes below is what had for first day of week: 01/01/yyyy
last day of week: 12/31/yyyy.
But it's not the way i want. i would like to calculate week number base on the definition above. Thanks
Expand|Select|Wrap|Line Numbers
  1. dteDate = Me.txtCollDate
  2. WeekNumber = Format(dteDate, "ww")
  3. Me.txtWeek = WeekNumber
  4.  
  5. If WeekNumber = 1 Then
  6. FirstDayOfWeek = "1/1/" & Year(dteDate)
  7. LastDayOfWeek = dteDate + (7 - Weekday(dteDate))
  8. Period = Format(FirstDayOfWeek, "mm/dd/yy") & "-" & Format(LastDayOfWeek, "mm/dd/yy")
  9. End If
  10.  
  11. If WeekNumber > 1 And WeekNumber < 53 Then
  12. FirstDayOfWeek = dteDate - (Weekday(dteDate) - 1)
  13. LastDayOfWeek = dteDate - Weekday(dteDate) + 7
  14. Period = Format(FirstDayOfWeek, "mm/dd/yy") & "-" & Format(LastDayOfWeek, "mm/dd/yy")
  15. End If
  16.  
  17. If WeekNumber = 53 Then
  18. WeekNumber = 52
  19. FirstDayOfWeek = (dteDate - (Weekday(dteDate) - 1)) - 7
  20. LastDayOfWeek = "12/31/" & Year(dteDate)
  21. Period = Format(FirstDayOfWeek, "mm/dd/yy") & "-" & Format(LastDayOfWeek, "mm/dd/yy")
  22. End If
  23.  
Jan 11 '08 #1
4 4682
Rabbit
12,516 Expert Mod 8TB
Hello All,
I would like to calculate weeknumber and period from first day of the week (sunday) and last day of the week (saturday) and week number falls from 1 to 52 only. If week number >= 53 then week number will equal to 52. And Last Day of First week is the first Saturday of January, as long as it falls at least four days into the month
Example:
if enter
Date WeekNo. Period
12/31/06 1 12/31/06-01/06/07
01/05/07 1 12/31/06-01/06/07
01/09/07 2 01/07/07-01/13/07
12/24/07 52 12/23/07-12/29/07
12/30/07 1 12/30/07-01/05/08
12/29/08 52 12/28/08-01/03/09
01/05/09 1 01/04/09-01/10/09

Any help would be appreciated. Thanks alots.

Codes below is what had for first day of week: 01/01/yyyy
last day of week: 12/31/yyyy.
But it's not the way i want. i would like to calculate week number base on the definition above. Thanks
Expand|Select|Wrap|Line Numbers
  1. dteDate = Me.txtCollDate
  2. WeekNumber = Format(dteDate, "ww")
  3. Me.txtWeek = WeekNumber
  4.  
  5. If WeekNumber = 1 Then
  6. FirstDayOfWeek = "1/1/" & Year(dteDate)
  7. LastDayOfWeek = dteDate + (7 - Weekday(dteDate))
  8. Period = Format(FirstDayOfWeek, "mm/dd/yy") & "-" & Format(LastDayOfWeek, "mm/dd/yy")
  9. End If
  10.  
  11. If WeekNumber > 1 And WeekNumber < 53 Then
  12. FirstDayOfWeek = dteDate - (Weekday(dteDate) - 1)
  13. LastDayOfWeek = dteDate - Weekday(dteDate) + 7
  14. Period = Format(FirstDayOfWeek, "mm/dd/yy") & "-" & Format(LastDayOfWeek, "mm/dd/yy")
  15. End If
  16.  
  17. If WeekNumber = 53 Then
  18. WeekNumber = 52
  19. FirstDayOfWeek = (dteDate - (Weekday(dteDate) - 1)) - 7
  20. LastDayOfWeek = "12/31/" & Year(dteDate)
  21. Period = Format(FirstDayOfWeek, "mm/dd/yy") & "-" & Format(LastDayOfWeek, "mm/dd/yy")
  22. End If
  23.  
Please use Code tags.

What you want is impossible. You have a range of dates, specifically the ones at the end of the year that lap over to the beginning of the next year, that can have more than one value but you give no way to decide between the two values. The following two records are from your example. They should either be both week number 1 or both be week number 52. They are the same type of dates but your result is inconsistent. It's arbitrary how you decide which number to assign it.

12/30/07 1 12/30/07-01/05/08
12/29/08 52 12/28/08-01/03/09
Jan 11 '08 #2
Hi, thanks for your reply Rabbit. So you mean there's no way I can calculate the weeknumber like this.

Sample: 12/30/07 1 12/30/07-01/05/08 , it calculated in this range is week number 1 because end of week is Saturday (01/05/08) and this is the first Saturday in January and it falls at least four days into the month such as (01/01/08, 01/02/08, 01/03/08, 01/04/08 and 01/05/08).

12/29/08 52 12/28/08-01/03/09, about this one it should calculate week number 52 even though (01/03/09) is still the first Saturday in January, however, it has only 3 days into that month (such as 01/01/09, 01/02/09, 01/03/09) , can't calculate week number 1 and should be calculated week number 52.

I tried too many ways but I couldn't get the result right. Does anyone else know please help, I'm very appreciated.
Jan 15 '08 #3
Rabbit
12,516 Expert Mod 8TB
This should do it, I didn't test it yet.
Expand|Select|Wrap|Line Numbers
  1. iif(((Year(DateVariable) mod 4 = 0 AND Format(DateVariable, "y") >= 364) OR (Year(DateVariable) mod 4 <> 0 AND Format(DateVariable, "y") >= 363)) AND Weekday(DateSerial(Year(DateVariable), 12, 31)) >= 4, 52, 1)
Jan 15 '08 #4
Rabbit
12,516 Expert Mod 8TB
After some testing, it looks like you should use:

Expand|Select|Wrap|Line Numbers
  1. IIf(Format(DateVariable, "ww") = Format(DateSerial(Year(DateVariable), 12, 31), "ww") Or Format(DateVariable, "ww") = 1, IIf(Weekday(DateSerial(Year(DateVariable), 12, 31)) >= 4, 52, 1), IIf(Weekday(DateSerial(Year(DateVariable), 1, 1)) >= 5, Format(DateVariable, "ww") - 1, Format(DateVariable, "ww")))
  2.  
Jan 15 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: CrystalDBA | last post by:
I am using SQL Server 2000. I need to query my database for all the contracts that came in during a certain time frame (user is prompted for reportingperiodid). Table - Periods Fields -...
5
by: DD | last post by:
Hi there, I am working on a sql-query and I need to know the weeknumber of a given date. Could anyone please help me with this. Thanks in advance, Arjen
0
by: Apple | last post by:
I need to create a form call "Receipt" that contain field to calculate payment - called " TotalPayment" = "PaymentFor" * "Period". In "PaymentFor", I suppose to use a combo box for 5 items...
10
by: bjaj | last post by:
Hi I have made this msgbox to display the current Week number, but it shows the wrong weekno. Can anyone tell me why ? msg = "WeekNumber: " & DatePart("WW", Date) MsgBox msg It shows...
2
by: Rustan | last post by:
Hi Im using GregorianCalendar to find out the current years week numbers. When the user chooses a week number in a dropdown i want to show that week in a table with the corresponding dates. For...
3
by: PeterW | last post by:
Hello, I have a table that contains fields for WeekNumber, OilReceived and OilRemaining. WeekNum OilRecd OilRem 4 1200 5 1000 2000 6 1800
4
by: Longkhi | last post by:
Hi Everybody. I'm developing a dentist program, which contains a MonthCalender. I would like to select a specific weeknumber, when I select a weeknumber in the MC. I can show the weeknumbers by...
1
by: peetersb | last post by:
Hi, I want ot make functions like this: int getFirtDayOfWeek(int weeknumber, int year); int getLastDayOfWeek(int weeknumber, int year); Firts I calculate the count of weeks like this: ...
4
by: shilpareddy2787 | last post by:
Hello, I have some total values, I want to calculate percenatge of these Total Values. I want to divide the total with No. Of working Days Excluding Saturdays and Sundays in a given period. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.