function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TykeTyke 

Calculating Week Number

Does anybody have a formula for calculating the week number of a given date?
SteveBowerSteveBower
Just for kicks I took a look at this question because I was curious. I thought it would be as simple as weeknumber=((today-(jan/1/this-year))/7)+1.

But, it's not.

Check out: http://webexhibits.org/calendars/week.html#SECTION00660000000000000000

for all the gory details. Could it be done in a formula, perhaps, but it ain't pretty! :-)

Fun, fun, fun, Steve Bower.
TykeTyke
If it was simple i wouldn't have posed the question :-)
Thanks for your interest.
john
Federico LarsenFederico Larsen
i use this and works ok.

Code:
function getWeekNr(fecha){
  Year = takeYear(fecha);
  Month = fecha.getMonth();
  Day = fecha.getDate();
  now = Date.UTC(Year,Month,Day+1,0,0,0);
  var Firstday = new Date();
  Firstday.setYear(Year);
  Firstday.setMonth(0);
  Firstday.setDate(1);
  then = Date.UTC(Year,0,1,0,0,0);
  var Compensation = Firstday.getDay();
  if (Compensation > 3) Compensation -= 4;
  else Compensation += 3;
  NumberOfWeek =  Math.round((((now-then)/86400000)+Compensation)/7);
  return fecha.getFullYear() + "/" + addZero(NumberOfWeek);
 }

function takeYear(theDate){
  x = theDate.getYear();
  var y = x % 100;
  y += (y < 38) — 2000 : 1900;
  return y;
 }

function addZero(vNumber){ 
  return ((vNumber < 10) – "0" : "") + vNumber 
 } 

 

RoperRoper
Hi,
    Does anyone know how i use the code posted on here to calculate the week number. Im new to Sforce and not even sure what type of code it is??

Many thanks

Dean
hisalesforcehisalesforce

Date today=System.today(); 
Date todaydate = System.today();
Date todaydateinstance = date.newinstance(todaydate.year(), todaydate.month(), todaydate.day());
Integer currentyear = todaydate.year();
Date startDate = date.newinstance(currentyear, 01, 01);
integer numberDaysDue = startDate.daysBetween(todaydateinstance);
Integer numberOfWeek = math.MOD(Integer.valueof(math.FLOOR( ( numberDaysDue )/7)),52)+1;