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
jason.wanjason.wan 

Calculate week of the month in Apex Classes

Anyone know of a way to get the Week of the Month (number 1 to 5 depending on month/calendar) in Apex Classes?

If there is custom code that someone has built, please let me know.  THANKS.

 

 

I only found Day of Year for the Salesforce datetime methods.

 

example:  Day of Year

 

Datetime myDate = datetime.newInstance(2008, 2, 5, 8, 30, 12);
system.assertEquals(myDate.dayOfYear(), 36);
Best Answer chosen by Admin (Salesforce Developers) 
SargeSarge

Hi,

 

   This was one of the utility code we had few months before. Here we assume that first week of the month is any number of  days between First day in the month and the weekend (i.e. Saturday) and the last week is any day between last day and last weekend.

 

 

public Integer currentWeekOfMonth( Date todaysDate){
	Date currentDate = todaysDate;
	Integer weekCount = 0;
	Integer startWeekResidue = 0;
	Integer endWeekResidue = 0;
	/*
           Calculating startWeekResidue
        */
        Date dt = currentDate.toStartOfMonth().addDays(-1); Date dtFirstWeekend = dt.toStartOfWeek().addDays(6); startWeekResidue = dt.daysBetween(dtFirstWeekend); 
        /*
            Calculating endWeekResidue
        */
        Date dtLastWeekend = currentDate.toStartOfWeek().addDays(-1); endWeekResidue = dtLastWeekend.daysBetween(currentDate); /*
            Counting the weeks
        */
        weekCount = (currentDate.day() - (startWeekResidue + endWeekResidue))/7; weekCount += (startWeekResidue > 0 ? 1:0)+(endWeekResidue > 0 ? 1:0); System.debug(weekCount); return weekCount; }

 

 

Hope this code helps.

 

 

All Answers

Jeremy_nJeremy_n

This will calculate an integer value for WeekOfMonth. The trick for me was that you have to cast the Integer for Day into a Double to make sure the Math.ceil function works properly.

 

Integer WeekOfMonth = Math.ceil((Double)(system.today().Day()) / 7).intValue();

 

Have fun!

 

Jeremy

 

 

SargeSarge

Hi,

 

   This was one of the utility code we had few months before. Here we assume that first week of the month is any number of  days between First day in the month and the weekend (i.e. Saturday) and the last week is any day between last day and last weekend.

 

 

public Integer currentWeekOfMonth( Date todaysDate){
	Date currentDate = todaysDate;
	Integer weekCount = 0;
	Integer startWeekResidue = 0;
	Integer endWeekResidue = 0;
	/*
           Calculating startWeekResidue
        */
        Date dt = currentDate.toStartOfMonth().addDays(-1); Date dtFirstWeekend = dt.toStartOfWeek().addDays(6); startWeekResidue = dt.daysBetween(dtFirstWeekend); 
        /*
            Calculating endWeekResidue
        */
        Date dtLastWeekend = currentDate.toStartOfWeek().addDays(-1); endWeekResidue = dtLastWeekend.daysBetween(currentDate); /*
            Counting the weeks
        */
        weekCount = (currentDate.day() - (startWeekResidue + endWeekResidue))/7; weekCount += (startWeekResidue > 0 ? 1:0)+(endWeekResidue > 0 ? 1:0); System.debug(weekCount); return weekCount; }

 

 

Hope this code helps.

 

 

This was selected as the best answer
Duncan_StewartDuncan_Stewart
I realize I'm quite late to this conversation, but FWIW, while Jeremy's formula will return the absolute week of the month, Sarge's is best in terms of a Calendar view, i.e. if each month 'row' starts on a Sunday, the actual start of the month could be in the middle of that week.
David Tissen 7David Tissen 7
The shortest answer is: 
 
Date.today().format('W');
Look at this: Salesforce uses SimpleDateFormat of JAVA (https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)