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
Stefaan Somers 11Stefaan Somers 11 

Apex returns wrong week

Today is 03/05/2021.

When I execute the following code, it returns week 19 instead of week 18
System.debug('Weeknumber : ' + System.now().format('w'));

Any idea on how I can resolve this?
ShivankurShivankur (Salesforce Developers) 
Hi Stefaan,

Please note, the year 2021s maximum week number is WN 52. 2021 starts on Friday, January 1st 2021 and ends on Friday, December 31st 2021. One year has up to 53 week numbers. The first week of the year (WN 1) is the week containing January 1st-3rd for this year.If we consider the same to be assumed as part of last year then according to which it should come as week number 18 via Apex.

But in Salesforce, Sunday January 3rd 2021 was the last day of week 1.And then if you consider it the week starting from Sunday it would come up as count to be WN 19 today.And which is correctly returned.

Please refer to below discussion for more detailed information on this:
https://salesforce.stackexchange.com/questions/125756/week-number-calculation-is-not-correct/125799
https://salesforce.stackexchange.com/questions/49355/how-to-get-correct-week-number-in-apex

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
Stefaan Somers 11Stefaan Somers 11
I understand, but is there a way then to modify the apex-code so that it indeed returns weekNumber 18 instead.
I'v read the other discussions, but it don't give any solutions on how to resolve this.

Doing -1 is not a good idea, because then the code will not work the next year.
ShivankurShivankur (Salesforce Developers) 
Hi Stefaan,

I agree to your point.The above explanation is to understand how does it works with week number calculation with Salesforce.

If you still need to return week number 18 from your code, you may need to modify the logic which you used earlier to get 19 with methods available in Salesforce.

You can modify following type of code logic, while trying to achieve the things as per your requirement:
Date startdate = date.parse('5/3/2021');

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();

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;
}

System.debug(numberOfWeek);
Test above code using Anonymous window in developer console.

This code would work similar to the single line code as posted earlier:
System.debug('Weeknumber : ' + System.now().format('w'));
It would be convenient of you make the changes in above logic as per your requirement and then you should be able to get the week number as needed.Additionally to suggest, you might need to add an additional check to see if its leap year or not and based on which you can return the results.

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
Stefaan Somers 11Stefaan Somers 11
Hi,

I've been trying to modify the code somewhat, but unfortunateley I don't succeed. Some of the test-methods still fail. Help would be welcome, so the final code can also be shared with the community.

Here is the class I created
/** * Created by stefaans on 4/05/2021. */ public with sharing class DateUtilities { public static Integer getWeekNumber(Date dateToProcess) { Date todaydateinstance = date.newinstance(dateToProcess.year(), dateToProcess.month(), dateToProcess.day()); Integer currentyear = dateToProcess.year(); Date yearstartdate = date.newinstance(currentyear, 01, 01); Date year2ndweek = yearstartdate.adddays(7).tostartofweek(); integer numberFirstWeek = yearstartdate.daysBetween(year2ndweek); System.debug('dateToProcess :' + dateToProcess); System.debug('numberFirstWeek :' + numberFirstWeek); integer numberDaysDue = year2ndweek.daysBetween(todaydateinstance); Integer numberOfWeek; Integer substractDays = 0; if (numberFirstWeek <= 3) { numberDaysDue = numberDaysDue-(numberFirstWeek-1); } System.debug('numberDaysDue :' + numberDaysDue); System.debug('Calcul (numberDaysDue) / 7) ->' + ((numberDaysDue) / 7)); System.debug('Calcul math.mod(numberDaysDue, 7) ->' + math.mod(numberDaysDue, 7)); System.debug('Calcul math.FLOOR((numberDaysDue) / 7) ->' + math.FLOOR((numberDaysDue) / 7)); System.debug('Calcul math.MOD(Integer.valueof(math.FLOOR((numberDaysDue) / 7)), 52) ->' + math.MOD(Integer.valueof(math.FLOOR((numberDaysDue) / 7)), 52)); if (math.mod(numberDaysDue, 7) == 0) { System.debug('Divisable by 7'); numberOfWeek = math.MOD(Integer.valueof(math.FLOOR((numberDaysDue) / 7)), 52) + 1; } else { numberOfWeek = math.MOD(Integer.valueof(math.FLOOR((numberDaysDue) / 7)), 52) + 2; } System.debug('numberofWeek :' + numberOfWeek); return numberOfWeek; } }

This is my test-class
/** * Created by stefaans on 4/05/2021. */ @IsTest public with sharing class DateUtilities_Test { @IsTest public static void testDate1() { Date dateToProcess = date.parse('31/12/2020'); System.assertEquals(53, DateUtilities.getWeekNumber(dateToProcess)); } @IsTest public static void testDate2() { Date dateToProcess = date.parse('31/12/2020'); System.assertEquals(53, DateUtilities.getWeekNumber(dateToProcess)); } @IsTest public static void testDate3() { Date dateToProcess = date.parse('3/5/2021'); System.assertEquals(18, DateUtilities.getWeekNumber(dateToProcess)); } @IsTest public static void testDate4() { Date dateToProcess = date.parse('4/5/2021'); System.assertEquals(18, DateUtilities.getWeekNumber(dateToProcess)); } @IsTest public static void testDate5() { Date dateToProcess = date.parse('4/1/2021'); System.assertEquals(1, DateUtilities.getWeekNumber(dateToProcess)); } @IsTest public static void testDate6() { Date dateToProcess = date.parse('31/12/2021'); System.assertEquals(52, DateUtilities.getWeekNumber(dateToProcess)); } @IsTest public static void testDate7() { Date dateToProcess = date.parse('1/1/2021'); System.assertEquals(0, DateUtilities.getWeekNumber(dateToProcess)); } }