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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which week number are we in? 2

Status
Not open for further replies.

liark

Programmer
Apr 18, 2000
36
GB
I need a bit of code to tell me the week number... can I use DateDiff?

Any help would be appreciated.

Thanks

Liark
mark@liark.com
 
Hey Liark, try my custom tag <cf_weeknumber>. Make sure to place the code below in a file, name the file &quot;weeknumber.cfm&quot; and place it in the custom tag dir on your server. Good luck!

<!---
cf_weeknumber by IQof188

Custom tag will display the correct weeknumber.
Input is a valid date, output is a variable called weeknumber (big surprise...)

Usage: <cf_weeknumber date=&quot;01/02/2001&quot;>
To output it: <cfoutput>#weeknumber#</cfoutput>

--->
<cfparam name=&quot;attributes.date&quot;>

<cfset YearType= DayOfWeek(&quot;01/01/&quot;&amp;DatePart(&quot;yyyy&quot;,attributes.date))-2>
<cfif YearType LT 0>
<cfset YearType=YearType+7>
</cfif>

<cfset WeekNumber=Ceiling((DayOfYear(attributes.date)+YearType)/7)>

<cfswitch expression=&quot;#YearType#&quot;>
<cfcase value=&quot;2&quot;>
<cfif WeekNumber IS 53 AND NOT IsLeapYear(DatePart(&quot;yyyy&quot;,attributes.date))>
<cfset WeekNumber=1>
</cfif>
</cfcase>
<cfcase value=&quot;4&quot;>
<cfset WeekNumber=WeekNumber-1>
<cfif WeekNumber IS 0>
<cfset WeekNumber=53>
</cfif>
</cfcase>
<cfcase value=&quot;5&quot;>
<cfset WeekNumber=WeekNumber-1>
<cfif WeekNumber IS 0>
<cfif IsLeapYear(DatePart(&quot;yyyy&quot;,attributes.date)-1)>
<cfset WeekNumber=53>
<cfelse>
<cfset WeekNumber=52>
</cfif>
</cfif>
</cfcase>
<cfcase value=&quot;6&quot;>
<cfset WeekNumber=WeekNumber-1>
<cfif WeekNumber IS 0>
<cfset WeekNumber=52>
</cfif>
</cfcase>
<cfdefaultcase>
<cfif WeekNumber IS 53>
<cfset WeekNumber=1>
</cfif>
</cfdefaultcase>
</cfswitch>
<cfset caller.WeekNumber=variables.Weeknumber> <webguru>iqof188</webguru>
 
I'm using:

<cf_weeknumber date=&quot;#DateFormat(Now(),&quot;mm/dd/yyyy&quot;)#&quot;>

as the current date, so I always get returned the current week number.

Liark
mark@liark.com
 
Isn't that what you want ;-)? The tag can output the weeknumber of any given date, and if you use Now() as the date, it will always give the current weeknumber... <webguru>iqof188</webguru>
 
Is there any reason you wouldn't just use the week() function?

GJ
 
Hey GunJack,

You are right, there is this Week() function, but I wrote the custom tag after hearing about trouble with this function.
It seems that for the US you can use this Week() function, but Europe and parts of Asia use another system to define the weeknumber (for instance, in Europe the week starts on monday, in the US it starts on a sunday).
The SetLocale function does not work properly for Week(), so that was no solution either.
The custom tag I wrote is applicable for Europe and Asia. The big question is: is Liark from the US or not??? :)

Regards.

<webguru>iqof188</webguru>
 
hehe, good one. The little details are often the ones that get you ;) Are there any other differences in standards that you're aware of? I never thought about such things causing problems.

GJ
 
Well, I am pretty sure that you are aware of all possible problems for Europeans working with dates, since they use another dateformat than Americans. Of course there are some date-formatting functions, but still in some cases you will have to find a workaround.
Well, isn't that what makes programming a challenge?

Regards.

<webguru>iqof188</webguru>
 
On the flip side of &quot;Which week number are we in?&quot;

How would you go about turning the week number back
into a date?

TIA
 
Nice one LKollodge :). I will think about this one, but one slight problem is that you won't be able to define a year, since most years (as for as I know ;-) ) have a week 12, week 25 etc. So it'll only work if you specify both weeknumber and year. I will see if I can write a tag for that.




<webguru>iqof188</webguru>
 
Ahhh, nice catch about &quot;defining the year&quot;, iqof188. :) I didn't think of that. I can
see where your custom tag will be extremely useful.

In the meantime, I have an Access database that contains the week number of when an order was placed. Does CF 4.5 provide a function to convert the week number back into a date?

TIA
 
Nope, CF 4.5 hasn't got a function for that. Aslo beware that the weeknumber differs among different countries, see other posts in this threat. Will try to work on the weeknumber to date problem...


<webguru>iqof188</webguru>
 
Thank you for all your help. It is deeply appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top