« Dear Apple | Home | Timer with a pause and resume »
September 8, 2009
Updated: Get current week of the year
I'm a nerd and so i like to play with code on my free time. Two days ago i went thru this post where there is a discussion on how to calculate the current week of the year. Here is my try:
The thing i don't like about the function posted on that site is that it is huge!
I haven't tested mine a lot but i trust on my visitors (you) to test it out and let me know if you find a bug or smth.
function wy(fecha:Date = null):uint {
var wms:uint=1000*60*60*24*7;
var d:Date = (!fecha) ? (new Date()): fecha;
var fd:Date=new Date(d.getFullYear(),0,1);
d.setDate(d.getDate() + (fd.getDay() - d.getDay()));
return ((d.getTime() - fd.getTime()) / wms) + 1;
}
trace (wy());
Coding is a lot of fun! :)
--fernando
Update 1: Thanks for testing it out, just fixed a bug on it :) Cheers!
Yeah, I saw the code on Clab, it was HUGE!
Love your 7 lines function, see you around.
very good solution :) .
But...
Current Date: 2009/09/14
trace( wy() ); // 38
trace( wy( new Date(2009,8,14) ) ); // 37
trace( wy( new Date(2009,8,14,0) ) ); // 37
trace( wy( new Date(2009,8,14,1) ) ); // 38
:)
@Otaku_Rzo:
You are right! We don't have week 0 neither. Method fixed! Thanks!