Solved

Week Number problem

Posted on 2010-08-18
4
353 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
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 …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

22 Experts available now in Live!

Get 1:1 Help Now