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
Jamz_2010Jamz_2010 

Week of the Year

Hi all,

 

I need to get the week of the year for a Salesforce project but am having some problems, I have tried using

 

 

System.Now().Format('w') ;

 

which should return the current week but this doesn't seem to work (it returns the wrong week of the year - for instance this week is week 21 and this returns week 22. For the business purposes the first week of the year is the week starting with the first Monday in January (this year that is w/c 04/01/2010). Does anyone know how you can do this in Salesforce?

 

Cheers

 

James

Pradeep_NavatarPradeep_Navatar

Salesforce method will calculate the week from the day 1 of the year. You would have to write the custom code to achieve this.

 

Hope the following javascript will help solve your problem.

 

<script>

 

   //To Make the current date

   var currentTime = new Date();

  

   //To Make a date of 1st of jan of year

   var onejan = new Date(currentTime.getFullYear(),0,1);

  

   //To find what the weekday of the 1st jan 

   var startDay = onejan.getDay();

  

   //Function To calculate the week Number of the year

   var a = Math.ceil((((currentTime - onejan) / 86400000) + currentTime.getDay()+1)/7);

  

   //If the value of a is 0 or 1 then the day is sunday or monday

   if(startDay ==0 || startDay ==1)

   {

    alert(a);

   }

   else

   {

     a = a-1;

     alert(a);

   }

   

 </script>

sau_2010sau_2010

Hi,

           This Apex code will help your problem.

 

  //To Make the currentdate

datetime myDateTime = datetime.now();

Integer  mydtstring = Integer.valueof(mydatetime.format('yyyy')); 

//To Make a date of 1st of jan of year 

datetime myDate = datetime.newInstance(mydtstring , 1, 1); 

//To find what the weekday of the 1st jan

String st = myDate.format('EEEE');

//Function To calculate the week Number of the year

integer aa = integer.valueof(System.Now().Format('w'));

system.debug('$$$'  + aa);
if(st == 'Sunday'  || st == 'Monday')

{    

system.debug(' @@@ 123 '  + aa );

}

else

{      

system.debug(' @@@  '  + integer.valueof(aa - 1));

}