Solved

Week Number problem

Posted on 2010-08-18
4
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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