# Week number calculation is not correct

I need to calculate week number from date.

I use this method:

``````DateTime.newInstance(year, month, day).format('w')
``````

But this returns week number 1 for 1 Jan 2016. In fact this date belongs to week 53 of 2015.

Is there any way to solve this? I need this to be European standard.

This gives you week number starting out from Sunday: How to get correct week number in Apex .You don`t have to worry about locale settings.

Finding the Week of the Year a Date Is In To find the number of a date’s week of the year, use this formula:

``````IF(CEILING((date-DATE(YEAR(date),1,1)+1)/7)>52,52,CEILING((date-DATE(YEAR(date),1,1)+1)/7))
``````

You can find the current week by determining how many days there have been in the current year and dividing that value by 7. The IF() statement ensures that the week number the formula returns doesn’t exceed 52. So if the given date is December 31 of the given year, the formula returns 52, even though it’s more than 52 weeks after the week of January.

Try this

``````MOD(FLOOR( ( CloseDate  -DATEVALUE("YYYY-MM-DD" ))/7)-48,53)+1
``````
• I need apex code Commented Jun 6, 2016 at 12:07
• oops, sorry misread! Commented Jun 6, 2016 at 12:26