Solved

Week Number problem

Posted on 2010-08-18
4
359 Views
Last Modified: 2012-06-27
I have the following function to produce the week number from a given date:

<CFFUNCTION NAME="GetWeekNo" RETURNTYPE="String">
  <CFARGUMENT NAME="MDate" DATATYPE="Date" REQUIRED="True">
  <CFIF Year(MDate) EQ 2012 OR Year(MDate) EQ 2017 OR Year(MDate) EQ 2023 OR
  Year(MDate) EQ 2034 OR Year(MDate) EQ 2040 OR Year(MDate) EQ 2045 OR
  Year(MDate) EQ 2051 OR Year(MDate) EQ 2062>
    <CFRETURN LZero(DatePart("ww",MDate))>
  <CFELSE>
    <CFRETURN LZero(DatePart("ww",MDate)-1)>
  </CFIF>
</CFFUNCTION>

The function works properly (the years mentioned in the function are to re-align the weeks because 01/01/year in those years produces week 00)

My boss now wants the week numbers to correspond with those in MSOutlook, with the first week of the year being the first full week.

I have tried manipulating the code in the function but I am getting in a mess!  Can someone please help me work it out?
0
Comment
Question by:jdthedj
  • 2
  • 2
4 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33471225
What happens when you just simply return:
LZero(DatePart("ww",MDate))

The 01/01/YYYY combinations that return week 00 are probably because they are at the end of the 53rd week in the previous year ... therefore, I suspect that you don't need to adjust the response. Guess you could test if it is zero before return and if so change to literal 53 or test LZero(DatePart("ww", DateAdd("d", -5, MDate))) ...
0
 
LVL 3

Author Comment

by:jdthedj
ID: 33471388
Thanks for the reply mwvisa1.  

I have tried every which way but there are always exceptions.  When I just use LZero(DatePart("ww",MDate)) 01/01/2013 returns week one instead of week 53.  01/01/2012 returns week one instead of week 52.  01/01/2011 does the same.  

I need a formula of some sort which knows when the full week is present at the start of a year, and then makes all the rest of the year follow logically on.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 33478060
Sorry for the delay ... was thinking of what would be your best approach given you have to not only find the adjusted week number for 1/1 when it doesn't start off the week but also maintain whatever adjustment is made through out calls for additional week numbers which would almost make you have to check for 1/1 on every single call to see if it is adjusted and then adjust your normal week number by that same amount. Actually that is probably it as you can take the year of the passed date and then determine the week number of 1/1 along with its day of the week. If it is week 1 and on day after what you consider the week start (i.e., Sunday or Monday) then you will know to -1 all responses. If it is 0 or 53, you can also determine what you want done.
0
 
LVL 3

Author Comment

by:jdthedj
ID: 33480808
I have gone for a very roundabout method - not the best approach.  Basically I take the date of the first Monday in the year, then loop to add a week until it reaches the target date.  At least it only has to loop 52 times at most.  The problem comes at year end/start when the week number returned = 0.  In this case I return the week number for 31st December.  The main thing is it works!

Thanks agian for you input.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now