Week number and week number difference in salesforce

Formula field:

Find Week Number :


MOD(FLOOR( (Date__c -DATEVALUE(Datefield ))/7),52)+1

Find Week Number difference (obvious) :

(MOD(FLOOR( (Date__c -DATEVALUE(Datefield ))/7),52)+1)  - (MOD(FLOOR( (Date__c -DATEVALUE(Datefield ))/7),52)+1)



Apex Code:

Find Week Number :

Integer weeknum = weeknumber(Date.newinstance(YourDatefield));
public Integer weeknumber(Date startdate){
Date todaydateinstance = date.newinstance(startdate.year(), startdate.month(), startdate.day());

Integer currentyear = startdate.year();

Date yearstartdate = date.newinstance(currentyear, 01, 01);
Date year2ndweek = yearstartdate.adddays(7).tostartofweek();

if(startdate<year2ndweek)
return 1;
integer numberDaysDue = year2ndweek.daysBetween(todaydateinstance);

Integer numberOfWeek;
if(math.mod(numberDaysDue,7)==0)
numberOfWeek = math.MOD(Integer.valueof(math.FLOOR( ( numberDaysDue )/7)),52)+1;
else
numberOfWeek = math.MOD(Integer.valueof(math.FLOOR( ( numberDaysDue )/7)),52)+2;
return numberOfWeek;

}


Find Week Number Difference:
Integer difference = weeknumber(Date.newinstance(YourDatefield2)) - weeknumber(Date.newinstance(YourDatefield1)) ;